您的位置:首页 > 移动开发 > Android开发

利用Android自定义View实现转盘旋转的效果

2010-05-29 18:57 1221 查看
Android的自定义View为开发者定义和使用个性化的View提供了很好的支持,想要使用自己定义的View,需要继承View类,并重写构造函数和onDraw()函数。onDraw函数在界面刷新时会被调用,通过线程控制可以实现动画的效果,这里提供一个用自定义View实现的类似幸运转盘的例子。

一、自定义的转盘View

package com.test.www;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;

//自定义的转盘View
public class zhuanpanView extends View implements Runnable{

//界面需要的图片
private Bitmap panpic;
private Bitmap panhandpic;
//旋转矩阵
private Matrix panRotate=new Matrix();
//平移矩阵
private Matrix panhandTrans=new Matrix();

private int x=0;

private boolean ifRotate=false;

public zhuanpanView(Context context, AttributeSet attrs) {

super(context, attrs);

Resources r=context.getResources();
//设置指针平移矩阵为按向量(160,160-指针的高度)平移
panhandTrans.setTranslate(160, 160-76);

//生成图片
panpic=BitmapFactory.decodeStream(r.openRawResource(R.drawable.pan));
panhandpic=BitmapFactory.decodeStream(r.openRawResource(R.drawable.panhand));

//用线程来刷新界面
Thread thread=new Thread(this);
thread.start();

}

//重写View类的onDraw()函数
@Override
protected void onDraw(Canvas canvas) {
//设置转盘旋转矩阵为以(160,160)坐标为圆心,旋转X角度
panRotate.setRotate(x, 160, 160);
canvas.drawBitmap(panpic, panRotate, null);

canvas.drawBitmap(panhandpic, panhandTrans, null);

}

//重写的run函数,用来控制转盘转动
public void run() {
try {

while(true){

if(ifRotate){

this.x+=25;
//这个函数强制UI线程刷新界面
this.postInvalidate();

Thread.sleep(50);

}

}

} catch (InterruptedException e) {

e.printStackTrace();
}

}

public void startRotate(){

this.ifRotate=true;

}

public void stopRotate(){

this.ifRotate=false;

}

}


二、layout中的对应的xml文件配置如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sky"
>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<com.test.www.zhuanpanView
android:id="@+id/zhuanpanView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</com.test.www.zhuanpanView>
</LinearLayout>

<RelativeLayout
android:layout_width="320px"
android:layout_height="110px"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/startButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="开始"
>
</Button>
<Button
android:id="@+id/stopButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="停止"
>
</Button>
</RelativeLayout>

</RelativeLayout>


其中com.test.www.zhuanpanView是自己定义的View组件的包路径,其它的属性配置和系统组件相同。

由于是自己定义的View,只能在运行模拟器后加载程序才能看到效果。

三、主Activity

package com.test.www;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
/** Called when the activity is first created. */

private void findViewAndButton(){

//自定义的View
final zhuanpanView panView=(zhuanpanView) this.findViewById(R.id.zhuanpanView);

//开始旋转的按钮
Button startButton=(Button) this.findViewById(R.id.startButton);
startButton.setOnClickListener(new OnClickListener(){

public void onClick(View v) {

panView.startRotate();

}

});
//停止旋转的按钮
Button stopButton=(Button) this.findViewById(R.id.stopButton);
stopButton.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {

panView.stopRotate();

}

});

}

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewAndButton();

}
}


这样就实现了可以控制旋转的转盘效果,修改onDraw函数可以实现更高级的动画效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: