Android的滑翔库与共享的元素转换工作 [英] Android Glide library not working with shared element transitions

查看:342
本文介绍了Android的滑翔库与共享的元素转换工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待采用到位的 / nostra13 / Android的通用-图片下载器>通用图像装载机但我遇到一个问题,关于共享的元素的转变。<​​/ p>

在我的简单的沙盒我创建使用UIL以下转变:<一href=\"https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-113333.mp4\">https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-113333.mp4

pretty简单,它工作正常。但是,当我用滑翔,它看起来并不那么好看:<一href=\"https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-114508.mp4\">https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-114508.mp4

下面是code我用

第一项活动:

 公共类MainActivity扩展BaseActivity {    @覆盖
    公共无效的onCreate(捆绑包){
        super.onCreate(包);
        的setContentView(R.layout.main);        最后ImageView的IV =(ImageView的)findViewById(R.id.main_image);        displayImageGlide(BaseActivity.IMAGE_URL,四,真正的);        Button按钮=(按钮)findViewById(R.id.main_button);
        button.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                意向意图=新意图(getApplicationContext(),DifferentActivity.class);
                startActivity(意向,IV);
            }        });
    }}

和第二个:

 公共类DifferentActivity扩展BaseActivity {    @覆盖
    公共无效的onCreate(捆绑包){
        super.onCreate(包);        的setContentView(R.layout.different);        ImageView的ImageView的=(ImageView的)findViewById(R.id.diff_image);        displayImageGlide(BaseActivity.IMAGE_URL,ImageView的,虚假的);
    }}

BaseActivity只包含 displayImageGlide

 公共无效displayImageGlide(字符串URL,ImageView的ImageView的){
    Glide.with(本).load(URL)
            .asBitmap()。变换(新CustomTransformation(本))
            .skipMemoryCache(真)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(ImageView的);
}私有静态类CustomTransformation扩展BitmapTransformation {    公共CustomTransformation(上下文的背景下){
        超级(上下文);
    }    @覆盖
    保护位图变换(BitmapPool池,位图toTransform,诠释outWidth,诠释outHeight){
        返回bitmapChanger(toTransform,outWidth,outHeight);
    }    @覆盖
    公共字符串的getId(){
        返回some_id_1
    }}私有静态位图bitmapChanger(位图位图,诠释desiredWidth,诠释desiredHeight){
    浮originalWidth = bitmap.getWidth();
    浮originalHeight = bitmap.getHeight();    漂浮的scaleX = desiredWidth / originalWidth;
    漂浮的scaleY = desiredHeight / originalHeight;    //使用两个尺度较大,以保持宽高比
    浮规模= Math.max(将scaleX,的scaleY);    字模=新的Matrix();    matrix.setScale(秤,秤);    //如果则scaleY较大,我们需要居中图像
    如果(将scaleX&LT;的scaleY){
        浮动TX =(规模* originalWidth - desiredWidth)/ 2F;
        matrix.postTranslate(-tx,0F);
    }    位图的结果= Bitmap.createBitmap(desiredWidth,desiredHeight,bitmap.getConfig()= NULL bitmap.getConfig():!?Bitmap.Config.ARGB_8888);
    帆布帆布=新的Canvas(结果);
    canvas.drawBitmap(位图,矩阵,新的油漆());
    返回结果;
}

我试着从本滑翔建设者缓存线(用于prevent从缓存什么我试图调整),但没有帮助。愿意采取滑翔,但它是怎样的一个大忌,如果这是不行的。如果任何人有一些投入我最好的AP preciate它,谢谢!


解决方案

我想你的code,得到了相同的结果,那么我想这下又实现:的https://github.com/$c$cpath/android_guides/wiki/Shared-Element-Activity-Transition

和做了一些改变,我认为是关键的位置:

该centerCrop应在ImageView的设置,如果我们用滑翔设置它会导致同样的错误。
activity_main.xml中

 &LT; ImageView的
      机器人:ID =@ + ID / ImageView的
      机器人:layout_width =match_parent
      机器人:layout_height =150dp
      机器人:layout_centerInParent =真
      机器人:transitionName =detail_image
      机器人:scaleType =centerCrop
    /&GT;

BaseActivity.java
对在MainActivity dontTransform应该是真实的和虚假的DifferentActivity

 公共无效displayImageGlide(字符串URL,ImageView的ImageView的,布尔dontTransform){
    如果(dontTransform){
        Glide.with(本).load(URL)
                .skipMemoryCache(真)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontTransform()
                .into(ImageView的);
        返回;
    }        Glide.with(本).load(URL)
                .skipMemoryCache(真)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(ImageView的);
   }

如果你需要我可以提供全面推行。

修改,看看它的样子: https://dl.dropboxusercontent.com/u/57688776/device-2015-06-23-153153.mp4

I'm looking to adopt the Glide library in place of Universal Image Loader but I'm running into a problem with regards to shared element transitions.

In my simple Sandbox I've created the following transition using UIL: https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-113333.mp4

Pretty simple, and it works fine. But when I used Glide, it doesn't look so nice: https://dl.dropboxusercontent.com/u/97787025/device-2015-06-18-114508.mp4

Here is the code I'm using

The first activity:

public class MainActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main);

        final ImageView iv = (ImageView) findViewById(R.id.main_image);

        displayImageGlide(BaseActivity.IMAGE_URL, iv, true);

        Button button = (Button) findViewById(R.id.main_button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), DifferentActivity.class);
                startActivity(intent, iv);
            }

        });
    }

}

And the second:

public class DifferentActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.different);

        ImageView imageView = (ImageView) findViewById(R.id.diff_image);

        displayImageGlide(BaseActivity.IMAGE_URL, imageView, false);
    }

}

BaseActivity just contains displayImageGlide

public void displayImageGlide(String url, ImageView imageView) {
    Glide.with(this).load(url)
            .asBitmap().transform(new CustomTransformation(this))
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(imageView);
}

private static class CustomTransformation extends BitmapTransformation {

    public CustomTransformation(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return bitmapChanger(toTransform, outWidth, outHeight);
    }

    @Override
    public String getId() {
        return "some_id_1";
    }

}

private static Bitmap bitmapChanger(Bitmap bitmap, int desiredWidth, int desiredHeight) {
    float originalWidth = bitmap.getWidth();
    float originalHeight = bitmap.getHeight();

    float scaleX = desiredWidth / originalWidth;
    float scaleY = desiredHeight / originalHeight;

    //Use the larger of the two scales to maintain aspect ratio
    float scale = Math.max(scaleX, scaleY);

    Matrix matrix = new Matrix();

    matrix.setScale(scale, scale);

    //If the scaleY is greater, we need to center the image
    if(scaleX < scaleY) {
        float tx = (scale * originalWidth - desiredWidth) / 2f;
        matrix.postTranslate(-tx, 0f);
    }

    Bitmap result = Bitmap.createBitmap(desiredWidth, desiredHeight, bitmap.getConfig() != null ? bitmap.getConfig() : Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bitmap, matrix, new Paint());
    return result;
}

I've tried removing the cache lines from the Glide builder (used to prevent it from caching what I'm trying to tweak) but that didn't help. Would love to adopt Glide, but it's kind of a deal breaker if this won't work. If anyone has some input I'd appreciate it, thanks!

解决方案

I tried your code and got the same result, then I tried another implementation following this: https://github.com/codepath/android_guides/wiki/Shared-Element-Activity-Transition

And made some changes, which I think are the key here:

The centerCrop should be set on the ImageView, if we set it with Glide it causes the same bug. activity_main.xml

    <ImageView
      android:id="@+id/ImageView"
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:layout_centerInParent="true"
      android:transitionName="detail_image"
      android:scaleType="centerCrop"
    />

BaseActivity.java On the MainActivity dontTransform should be true and on DifferentActivity false.

    public void displayImageGlide(String url, ImageView imageView, Boolean dontTransform) {
    if (dontTransform) {
        Glide.with(this).load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontTransform()
                .into(imageView);
        return;
    }

        Glide.with(this).load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
   }

If you need I can provide the full implementation.

Edit, see how it looks: https://dl.dropboxusercontent.com/u/57688776/device-2015-06-23-153153.mp4

这篇关于Android的滑翔库与共享的元素转换工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆