我今天也只是刚好看到这个,看到网友实现了一个转盘的效果,但试了一下好像不太行,所以就修改了一下程序最终实现转盘的效果,我对Android 绘画这一块用得不多,一般在开发游戏的时候可能会用得较多,所以很多地方都不太清楚。
下面是转盘的效果图:
布局:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/back"
- >
- <com.wwj.turnplate.TurnPlateView
- android:id="@+id/turnplate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- />
- <Button
- android:id="@+id/btnStart"
- android:layout_width="70dp"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:text="开始" />
- <Button
- android:id="@+id/btnStop"
- android:layout_width="70dp"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:text="结束" />
- </RelativeLayout>
自定义View
/2013.10.16_TurnPlate_Demo/src/com/wwj/turnplate/TurnPlateView.java
- package com.wwj.turnplate;
- 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;
- public class TurnPlateView extends View implements Runnable{
- // 界面需要的图片
- private Bitmap turnPlatePic; // 转盘图片
- private Bitmap pointerPic; // 指针图片
- // 旋转矩阵
- private Matrix panRotate = new Matrix();
- // 平移矩阵
- private Matrix pointerTranslate = new Matrix();
- private int x = 0;
- private boolean ifRoate = false;
- public TurnPlateView(Context context, AttributeSet attrs) {
- super(context, attrs);
- Resources r = context.getResources();
- // 生成图片
- turnPlatePic = BitmapFactory.decodeStream(r.openRawResource(R.drawable.turnplate));
- pointerPic = BitmapFactory.decodeStream(r.openRawResource(R.drawable.pointer));
- // 用线程刷新界面
- Thread thread = new Thread(this);
- thread.start();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.translate(75, 100);
- canvas.drawBitmap(turnPlatePic, panRotate, null);
- canvas.translate(turnPlatePic.getWidth() / 2 - pointerPic.getWidth() / 2, 20);
- canvas.rotate(x, pointerPic.getWidth() / 2,pointerPic.getHeight() - 35);
- canvas.drawBitmap(pointerPic, pointerTranslate, null);
- }
- @Override
- public void run() {
- try {
- while(true) {
- if (ifRoate) {
- this.x += 25;
- //这个函数强制UI线程刷新界面
- this.postInvalidate();
- Thread.sleep(50);
- }
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void startRoate() {
- this.ifRoate = true;
- }
- public void stopRotate() {
- this.ifRoate = false;
- }
- }
主Activity
- package com.wwj.turnplate;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity implements OnClickListener{
- private TurnPlateView plateView;
- private Button btnStart;
- private Button btnStop;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- plateView = (TurnPlateView) findViewById(R.id.turnplate);
- btnStart = (Button) findViewById(R.id.btnStart);
- btnStop = (Button) findViewById(R.id.btnStop);
- btnStart.setOnClickListener(this);
- btnStop.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch(v.getId()) {
- case R.id.btnStart:
- plateView.startRoate();
- break;
- case R.id.btnStop:
- plateView.stopRotate();
- break;
- }
- }