Android记录9--实现转盘效果,颠覆认知

本文介绍了如何在Android中创建一个自定义的转盘视图,包括布局设计和自定义View类`TurnPlateView`的实现,通过矩阵操作实现转盘及指针的旋转效果。同时提供了开始和结束旋转的按钮控制。完整代码和更多学习资料可在链接中获取。
摘要由CSDN通过智能技术生成

布局:

<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值