Android图像动画使用多个图像

时间:2014-12-05 12:11:19

标签: android animation android-animation

我正在寻找动画自由代码,这将帮助我使用一组图像进行360度动画(大约60个图像用于水平旋转)。此外,图像也在SDCARD中,而不是在应用程序中。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以创建自定义视图并覆盖onDraw(Canvas)方法并绘制所需图像的位图。

您可以跟踪当前"方向"在度和计算哪些应使用Canvas.drawBitmap(位图,矩阵,油漆)显示在什么位置。

以下是根据手机方向移动一张图片的示例。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class SkySurfaceView extends SurfaceView implements OnTouchListener {

    private static final String TAG = SkySurfaceView.class.getSimpleName();
    private double viewPortX, viewPortY;
    private int starX, starY;
    private Bitmap target;
    private int targetWidth, targetHeight;
    private int vpWidth, vpHeight;
    private Matrix skyMatrix, skyMatrix2;
    private Matrix skyMatrix3;
    private Bitmap star;
    private Paint paint;
    private Rect touchRect, touchRect2, touchRect3;

    public SkySurfaceView(Context context, AttributeSet attr) {
        super(context, attr);
        viewPortX = viewPortY = 0;
        skyMatrix = new Matrix();
        skyMatrix2 = new Matrix();
        skyMatrix3 = new Matrix();

        paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setDither(true);

        paint.setColor(Color.RED);
        Options opt = new Options();
        opt.inSampleSize = 2;
        opt.inScaled = false;
        opt.inPreferredConfig = Config.RGB_565;
        star = BitmapFactory.decodeResource(getResources(), R.drawable.star,
                                            opt);
        touchRect = new Rect(0, 0, star.getWidth(), star.getHeight());
        touchRect2 = new Rect(0, 0, star.getWidth(), star.getHeight());
        touchRect3 = new Rect(0, 0, star.getWidth(), star.getHeight());

        this.setWillNotDraw(true);
        setLayerType(View.LAYER_TYPE_HARDWARE, null);
        this.setOnTouchListener(this);
        getHolder().setFormat(PixelFormat.RGB_565);
    }

    public void setTarget(Bitmap b) {
        target = b;

        targetHeight = target.getHeight();
        targetWidth = target.getWidth();
    }

    public void init() {

        this.starX = (int) (vpWidth * Math.random()) + vpWidth;
        this.starY = (int) ((vpHeight - star.getHeight()) * Math.random());
        Log.i(TAG, "drawn star on " + starX + "," + starY);
        Canvas c = new Canvas();
        Log.i(TAG,
              "target dimension is " + target.getWidth() + "x"
              + target.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(target.getWidth(),
                                            target.getHeight(), Config.RGB_565);
        c.setBitmap(bitmap);
        c.drawBitmap(target, 0, 0, paint);
        c.drawBitmap(star, starX, starY, paint);
        c.drawBitmap(star, starX - targetWidth, starY, paint);
        target.recycle();
        setTarget(bitmap);
        setWillNotDraw(false);

    }

    @Override
    public boolean performClick() {
        super.performClick();
        return false;
    }

    /**
     *
     * @param x
     *            - [-1:1]
     * @param y
     *            - [-1:1]
     */
    public void setViewPort(double x, double y) {

        viewPortX = x;
        viewPortY = y;

        int tempX = (int) (targetWidth * (viewPortX));
        int tempY = (int) (targetHeight * (viewPortY - 1));

        tempY = Math.max(tempY, -(targetHeight - vpHeight) / 2);
        tempY = Math.min(tempY, +(targetHeight - vpHeight / 2));

        Log.i(TAG,
              String.format("%d %d , %d %d, %d %d", tempX, tempY, tempX
                            - targetWidth, tempY, tempX + targetWidth, tempY));

        skyMatrix.reset();
        skyMatrix.postTranslate(tempX, tempY);
        skyMatrix2.reset();
        skyMatrix2.postTranslate(tempX - targetWidth, tempY);
        skyMatrix3.reset();
        skyMatrix3.postTranslate(tempX + targetWidth, tempY);

        int xx = (tempX + starX);
        while (xx < targetWidth) {
            xx += targetWidth;
        }
        touchRect.offsetTo(xx % targetWidth, (tempY + starY) % targetHeight);
        touchRect2.offsetTo(xx % targetWidth - targetWidth, (tempY + starY)
                            % targetHeight);
        touchRect3.offsetTo(xx % targetWidth + targetWidth, (tempY + starY)
                            % targetHeight);
        postInvalidate();
    }

    public void setViewportSize(int x, int y) {
        vpWidth = x;
        vpHeight = y;
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        c.drawBitmap(target, skyMatrix, paint);
        c.drawBitmap(target, skyMatrix2, paint);
        c.drawBitmap(target, skyMatrix3, paint);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float ex = event.getX();
        float ey = event.getY();

        Log.i(TAG, "touched " + ex + " " + ey);
        if (touchRect.contains((int) ex, (int) ey)) {
            if (listener != null) {
                listener.onCaptured();
            }
        } else if (touchRect2.contains((int) ex, (int) ey)) {
            if (listener != null) {
                listener.onCaptured();
            }
        } else if (touchRect3.contains((int) ex, (int) ey)) {
            if (listener != null) {
                listener.onCaptured();
            }
        }

        return false;
    }


    public void setListener(OnStarCaptureListener listener) {
        this.listener = listener;
    }

}

在这个例子中,&#34; target&#34;是天空的形象。每当调用setViewPort(x和y [-1:1])时,图像的绘图矩阵就会更新,并且&#34; invalidate()&#34;叫做。 onDraw()方法调用将绘制&#34; target&#34;与抵消。