import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private Button btstart;
private AnimationDrawable andrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.imageView1);
btstart=(Button) findViewById(R.id.btstart);
//给ImageView设置背景资源,资源为动画的xml文件
iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.frame_anim));
andrawable=(AnimationDrawable) iv.getBackground();
btstart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(andrawable.isRunning()){
andrawable.stop();
}else{
andrawable.stop();
andrawable.start();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/firefox_animation_0" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_1" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_2" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_3" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_4" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_5" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_6" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_7" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_8" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_9" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_10" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_11" android:duration="500" />
<item android:drawable="@drawable/firefox_animation_12" android:duration="500" />
</animation-list>
图片的放大缩小,旋转
package com.view;
import com.action.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class BitmapCanverView extends View {
private int op;
private float wscale = 1.0f;
private float hscale = 1.0f;
private int agle;
private Bitmap bm;
private Matrix m;
private Paint paint;
public int getOp() {
return op;
}
public void setOp(int op) {
this.op = op;
}
public Bitmap getBm() {
return bm;
}
public void setBm(Bitmap bm) {
this.bm = bm;
}
public BitmapCanverView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
System.out.println("1--");
}
public BitmapCanverView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
System.out.println("2---");
bm = BitmapFactory.decodeResource(getResources(), R.drawable.m9);
}
public BitmapCanverView(Context context) {
super(context);
// TODO Auto-generated constructor stub
System.out.println("3---");
}
@Override
protected void onDraw(Canvas canvas) {
switch (op) {
case 1: {// 放大
// 获取放大的比例
wscale = wscale * 1.25f;
hscale = hscale * 1.25f;
// 创建 Matrix对象
m = new Matrix();
m.postScale(wscale, hscale);
paint = new Paint();
canvas.drawBitmap(bm, m, paint);
}
break;
case 2: {// 缩小
// 获取缩小的比例
wscale = wscale * 0.8f;
hscale = hscale * 0.8f;
// 创建 Matrix对象
m = new Matrix();
m.postScale(wscale, hscale);
paint = new Paint();
canvas.drawBitmap(bm, m, paint);
}
break;
case 3: {// 左转
// 设置旋转的角度
agle -= 5;
m = new Matrix();
// bm.getWidth()/4, bm.getHeight()/4用于计算图片的中心点
m.postRotate(agle, bm.getWidth() / 2, bm.getHeight() / 2);
paint = new Paint();
canvas.drawBitmap(bm, m, paint);
}
break;
case 4: {// 右转
// 设置旋转的角度
agle += 5;
m = new Matrix();
// bm.getWidth()/4, bm.getHeight()/4用于计算图片的中心点
m.postRotate(agle, bm.getWidth() / 2, bm.getHeight() / 2);
paint = new Paint();
canvas.drawBitmap(bm, m, paint);
}
break;
default: {
bm = BitmapFactory.decodeResource(getResources(), R.drawable.m9);
paint=new Paint();
m=new Matrix();
canvas.drawBitmap(bm, m, paint);
}
break;
}
}
}