动画 ImageView 的位图位置

Animate ImageView's Bitmap Position

我有一个带 width = 1080height = 1920

的 ImageView

我有一个位图 width = 3340height = 1920

我将imageView的缩放类型设置为MATRIX。这样位图就不会缩放到imageView

    imageView.setScaleType(ImageView.ScaleType.MATRIX);

我想使位图从左到右动画化,反之亦然。

可以做吗?有什么想法吗?

根据此 link http://www.sanfoundry.com/java-android-program-animante-bitmap/ 中的示例,您需要在 onDraw() 中使用 canvas 移动图像。所以,按照这个 link,也许这会对你有所帮助。并让我知道任何问题。

请在您的项目中创建以下自定义视图 class

package com.company.xyz;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by rohitp on 12/10/2015.
 */
public class CustomImageView extends View {

    Bitmap bm;
    int x, y;
    boolean flag = true;

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

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        try{
            int src_resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
            bm = getDrawable(getResources(),src_resource);
        }catch (Exception e){

        }
    }

    public static Bitmap getDrawable(Resources res, int id){
        return BitmapFactory.decodeResource(res, id);
    }

    public void setBm(Bitmap bm) {
        this.bm = bm;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        if(bm != null){
            if(flag){
                if (x < (getMeasuredWidth()-bm.getWidth())) {
                    x += 10;
                    Log.e("Custom",x+""+getMeasuredWidth());
                }
                else {
                    Log.e("Custom",x+" false");
                    flag = false;
                }
            }else{
                if (x >0 ) {
                    x -= 10;
                    Log.e("Custom",x+" -");
                }
                else {
                    flag = true;
                    Log.e("Custom",x+" true");
                }
            }


            canvas.drawBitmap(bm, x, y, new Paint());
        }
        invalidate();//calls this method again and again
    }
}

创建一个布局 xml 如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.company.xyz.CustomImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/animate_pic"
        android:src="@drawable/test">
    </com.company.xyz.CustomImageView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

而且您还可以在运行时通过代码更改位图

((CustomImageView) findViewById(R.id.animate_pic)).setBm(bm)