matrix 实现动画3D旋转效果

    xiaoxiao2023-03-24  2

    首先看下效果:

    大概效果就是这样 如果是你需要的 就继续往下看 

    如果是简单实现一个那就容易了 那也不需要用到matrix

    代码如下:

    AnimatorSet animatorSetCionOne=new AnimatorSet(); ObjectAnimator objectAnimatorCionOneRotation=ObjectAnimator.ofFloat(mImageViewCionOne,"rotation",250f,-30f,0f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneRotationX=ObjectAnimator.ofFloat(mImageViewCionOne,"rotationX",-200f,0f,180,0f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneRotationY=ObjectAnimator.ofFloat(mImageViewCionOne,"rotationY",169f,-60f,0f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneTranslationY=ObjectAnimator.ofFloat(mImageViewCionOne,"translationY",150f,0f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneTranslationX=ObjectAnimator.ofFloat(mImageViewCionOne,"translationX",200f,0f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneScaleX= ObjectAnimator.ofFloat(mImageViewCionOne,"scaleX",0,1f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneScaleY= ObjectAnimator.ofFloat(mImageViewCionOne,"scaleY",0,1f).setDuration(mLongLongDurationTime); ObjectAnimator objectAnimatorCionOneAlpha= ObjectAnimator.ofFloat(mImageViewCionOne,"alpha",0,1.0f).setDuration(mDurationTime); animatorSetCionOne.setStartDelay(600); animatorSetCionOne.playTogether(objectAnimatorCionOneTranslationY,objectAnimatorCionOneTranslationX,objectAnimatorCionOneScaleX, objectAnimatorCionOneRotation,objectAnimatorCionOneRotationX,objectAnimatorCionOneRotationY,objectAnimatorCionOneScaleY,objectAnimatorCionOneAlpha);

    分别设置一下绕X轴,Y轴,Z轴 旋转旋转 再搞点位移,再搞点缩放 再搞点透明渐变就可以了 很炫酷了 

    但是 如果像我这样 有十几个金币啥的 就比较烦了  代码写的老多了

    所以 需求就是学习的动力 

    下面就开始上代码了

    import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Camera; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PaintFlagsDrawFilter; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CoinsView extends View { private int times = 0; private int coinsNumber = 1; private int moveXLenth = 0; private int moveYLenth = 0; private int mCenterX = 650; private int mCenterY = 700; private final static String X_KEY = "Xpos"; private final static String Y_KEY = "Ypos"; private List<Map<String, Integer>> mListPoint = new ArrayList<Map<String, Integer>>(); //设置每个目标的图片 private int[] BITMAPLIST = {R.drawable.ic_main_cion, R.drawable.ic_cion_two, R.drawable.ic_cion_three, R.drawable.ic_cion_four, R.drawable.ic_cion_five, R.drawable.ic_cion_six, R.drawable.ic_cion_seven,R.drawable.ic_cion_eight}; private int[] RECTWITH = {100, 200, 100, 110, 150}; private Matrix matrix; //作用矩阵 private Camera camera; Paint mPaint = new Paint(); public CoinsView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public CoinsView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CoinsView(Context context) { super(context); init(); } private void init() { /*showBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.flower); centerX = showBmp.getWidth()/2 ; centerY = showBmp.getHeight()/2 ;*/ matrix = new Matrix(); camera = new Camera(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(Color.YELLOW); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); canvas.translate(500, 600); for (int i = 0; i < coinsNumber; i++) { if (times > 0) { //取出各种不同的图片 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), BITMAPLIST[i]); // Rect bitmaprect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); //计算位移 moveXLenth = ((mListPoint.get(i).get(X_KEY) - mCenterX) * times) / 100; moveYLenth = ((mListPoint.get(i).get(Y_KEY) - mCenterY) * times) / 100; float proportion=(float) times/100; Log.d("gggggggggg", "onDraw: "+proportion); //matrix要做出3D旋转效果很麻烦,所以我们借助了camera这个类 camera.save(); //设置绕xyz三个轴都旋转,其中times是通过valueAnimator动态传进来的值 camera.rotate(times * 360, 2 * times * 360, 360 * times / 100); //设置camera作用矩阵 camera.getMatrix(matrix); camera.restore(); //设置图片缩放 matrix.postScale(proportion*0.5f, proportion*0.5f); //设置翻转中心点 matrix.preTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2); matrix.postTranslate(bitmap.getWidth() / 2, bitmap.getWidth() / 2); matrix.postTranslate(moveXLenth, moveYLenth); //设置透明渐变 /*ColorMatrix mColorMatrix = new ColorMatrix(new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, proportion, 0,//这里是设置透明度的 上面三个分别是设置RGB的 这个属于A }); //把透明度设置上去 mPaint.setColorFilter(new ColorMatrixColorFilter(mColorMatrix));*/ //通过matrix绘制图片 canvas.drawBitmap(bitmap, matrix, mPaint); // Rect rect = new Rect(mCenterX + moveXLenth, mCenterY + moveYLenth, mCenterX + RECTWITH[i] + moveXLenth, mCenterY + moveYLenth + RECTWITH[i]); // canvas.drawBitmap(bitmap, bitmaprect, rect, mPaint); } } } //设置目标点的集合 public void setCoinsPointList(List<Map<String, Integer>> list) { if (list.size() > 0) { mListPoint = list; coinsNumber = list.size(); } } //让动画开始动起来 public void setDurationTime(int time) { times = time; invalidate(); }

    这是自定义的一个类 

    使用的话如下:

    布局文件:

    <com.example.yanwei.myapplication.CoinsView android:visibility="visible" android:id="@+id/coin_view" android:layout_width="match_parent" android:layout_height="match_parent" />

    代码中调用顺序:

    mCoinsView = (CoinsView) findViewById(R.id.coin_view); //设值给控件

    private void initDate(){ Map<String, Integer> temp0 = new HashMap<String, Integer>(); temp0.put(X_KEY,DisplayUtils.dp2Px(this,150));//这个可以直接设值 temp0.put(Y_KEY,DisplayUtils.dp2Px(this,180)); mListPoint.add(temp0); Map<String, Integer> temp1 = new HashMap<String, Integer>(); temp1.put(X_KEY, 180); temp1.put(Y_KEY, 300); mListPoint.add(temp1); Map<String, Integer> temp2 = new HashMap<String, Integer>(); temp2.put(X_KEY, 100); temp2.put(Y_KEY,1200); mListPoint.add(temp2); Map<String, Integer> temp3 = new HashMap<String, Integer>(); temp3.put(X_KEY, 400); temp3.put(Y_KEY, 400); mListPoint.add(temp3); Map<String, Integer> temp4 = new HashMap<String, Integer>(); temp4.put(X_KEY, 800); temp4.put(Y_KEY, 200); mListPoint.add(temp4); Map<String, Integer> temp5 = new HashMap<String, Integer>(); temp5.put(X_KEY, 800); temp5.put(Y_KEY, 1200); mListPoint.add(temp5); Map<String, Integer> temp6 = new HashMap<String, Integer>(); temp6.put(X_KEY, 500); temp6.put(Y_KEY, 220); mListPoint.add(temp6); Map<String, Integer> temp7 = new HashMap<String, Integer>(); temp7.put(X_KEY, 150); temp7.put(Y_KEY, 350); mListPoint.add(temp7); mCoinsView.setCoinsPointList(mListPoint); }

    开启valueAnimator,动态传值给控件

    ValueAnimator animator = ValueAnimator.ofInt(0,100); animator.setStartDelay(1500); animator.setDuration(2000); animator.setRepeatMode(ValueAnimator.RESTART); animator.start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int curValue = (int)animation.getAnimatedValue(); mCoinsView.setDurationTime(curValue); } });

    好像差不多了 

    接下来自己摸索

    转载请注明原文地址: https://ju.6miu.com/read-1200069.html
    最新回复(0)