布局:
<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=“结束” />
自定义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(turnPl
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
atePic.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