[Android]2D绘图与动画-graphics的Canvas

代码

1. CanvasBaseOperateActivity.java

package home.lee.canvas2ddemo1;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;

public class CanvasBaseOperateActivity extends Activity implements AnimationListener{

private Bitmap mBitmap;
private AnimationDrawable animationDrawable;
private ImageView animationImage;
private Animation animation;
private EditText animationEditText;
private Animation animation1;
private Animation animation2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//requestWindowFeature(Window.FEATURE_PROGRESS);
//setContentView(new MyView(this));

//帧动画-anim路径-连续静态图片
//animationDrawable = (AnimationDrawable) getResources().getDrawable(R.anim.anim_frame);
//animationImage = (ImageView)findViewById(R.id.animationimage);
//animationImage.setBackgroundDrawable(animationDrawable);
//animationDrawable.start();

//补间动画
//animation = AnimationUtils.loadAnimation(this, R.anim.anim_tween1);
//animationEditText = (EditText)findViewById(R.id.animationedittext);
//animationEditText.setAnimation(animation);
//animation.start();
//animationEditText.startAnimation(animation);
//多个动画文件循环播放
animation1 = AnimationUtils.loadAnimation(this, R.anim.anim_tween1);
animation2 = AnimationUtils.loadAnimation(this, R.anim.anim_tween2);
animationImage = (ImageView)findViewById(R.id.animationimage);
animation1.setAnimationListener(this);
animation2.setAnimationListener(this);
animationImage.startAnimation(animation1);

}

@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
if(animation1.hasEnded()){
animationImage.startAnimation(animation2);
}else if(animation2.hasEnded()){
animationImage.startAnimation(animation1);
}
}

// public class MyView extends View{
//
// private Paint paint2;
// private float[] textsize;
// private Paint paint3;
// private RectF rectf1;
// private boolean useCenter = true;
// private RectF rectf2;
// private Paint paint4;
// private InputStream inputStream;
// private Bitmap bitmap;
// private Paint paint5;
//
//
// public MyView(Context context) {
// super(context);
// //绘制位图的方法
// //1. 转换为位图,显示出来
// mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// //2. 通过输入流获得位图
// inputStream = getResources().openRawResource(R.drawable.ic);
// BitmapFactory.Options opts = new BitmapFactory.Options();
// opts.inSampleSize = 2;//缩放成原来图形大小的4分之一
// bitmap = BitmapFactory.decodeStream(inputStream, null, opts);
//
// }
//
// @Override
// protected void onDraw(Canvas canvas) {
// //配置画板
// canvas.drawColor(Color.BLACK);
// //图片资源-位置
// canvas.drawBitmap(mBitmap, 20, 20, null);
//
// //画字
// paint2 = new Paint();
// textsize = new float[]{12, 15, 18, 21, 24};
// int y = 0;
// for(int i = 0; i < textsize.length; i++){
// paint2.setTextSize(textsize[i]);
// paint2.setColor(Color.BLUE);
//
// canvas.drawText("Android(宽度: " + paint2.measureText("Android") + ")", 30, 10 + y, paint2);
// y += paint2.getTextSize() + 10;
// }
//
// //画圆形
// paint3 = new Paint();
// paint3.setColor(Color.RED);
// paint3.setStyle(Style.STROKE);//画笔-空心
// rectf1 = new RectF(0, 70, 100, 130);//矩形-参数(leftx, topy, rightx, bottomy)为坐标值
// canvas.drawArc(rectf1, 0, 360, useCenter , paint3);//其实是椭圆
//
// paint4 = new Paint();
// paint4.setColor(Color.BLUE);
// rectf2 = new RectF(20, 70, 80, 130);
// canvas.drawArc(rectf2, 0, 360, useCenter, paint4);//圆形
//
// paint5 = new Paint();
// paint5.setColor(Color.WHITE);
// paint5.setStyle(Style.FILL);
// canvas.drawPosText("圆形", new float[]{20, 80, 50, 110}, paint5);
//
// //2-结果
// canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(50, 50, 210, 130), null);
// //旋转图片
// Matrix matrix = new Matrix();
// matrix.setRotate(120, 160, 240);//旋转
// canvas.setMatrix(matrix);
// paint5.setAlpha(30);//透明度
// canvas.drawBitmap(bitmap, 10, 10, paint5);
//
// //3. Drawable.draw方法绘位图
// Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
// drawable.setBounds(100, 170, 150, 220);
// drawable.draw(canvas);
// invalidate();//不断刷新View
// canvas.translate(50, 50);//移动
// super.onDraw(canvas);
//
//
//
// }
// }
}

2. res/anim/..文件

2. 1  anim_frame.xml - 帧动画

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
<item android:drawable="@drawable/f1" android:duration="1000"></item>
<item android:drawable="@drawable/s2" android:duration="1000"></item>
<item android:drawable="@drawable/t3" android:duration="1000"></item>
<item android:drawable="@drawable/f4" android:duration="1000"></item>
<item android:drawable="@drawable/f5" android:duration="1000"></item>
</animation-list>

2.2 anim_tween1.xml - 补间动画1

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<rotate
android:fromDegrees="0"
android:toDegrees="270"
android:pivotX="50"
android:pivotY="50"
android:duration="3000" />
</set>

2.3 anim_tween2.xml - 补间动画2

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:repeatCount="2"
android:repeatMode="reverse"
android:duration="1500" />
</set>

下载代码资源代码

原文地址:https://www.cnblogs.com/webapplee/p/3771839.html