Android自定義View實(shí)現(xiàn)QQ音樂中圓形旋轉(zhuǎn)碟子
QQ音樂中圓形旋轉(zhuǎn)碟子的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
思路分析:
1、在onMeasure中測量整個(gè)View的寬和高后,設(shè)置寬高
2、獲取我們r(jià)es的圖片資源后,在ondraw方法中進(jìn)行繪制圓形圖片
3、通過Handler發(fā)送Runnable來啟動(dòng)旋轉(zhuǎn)線程(如果只想做圓形頭像的話,這步可以去掉)
4、在布局中使用我們的View
效果圖:

貼出我們的變量信息:
//view的寬和高 int mHeight = 0; int mWidth = 0; //圓形圖片 Bitmap bitmap = null; //圓形圖片的真實(shí)半徑 int radius = 0; //旋轉(zhuǎn)動(dòng)畫的矩形 Matrix matrix = new Matrix(); //旋轉(zhuǎn)動(dòng)畫的角度 int degrees = 0;
步驟一:測量整個(gè)View的寬和高后,設(shè)置寬高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//測量整個(gè)View的寬和高
mWidth = measuredWidth(widthMeasureSpec);
mHeight= measuredHeight(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
private int measuredWidth(int widthMeasureSpec) {
int Mode = MeasureSpec.getMode(widthMeasureSpec);
int Size = MeasureSpec.getSize(widthMeasureSpec);
if (Mode == MeasureSpec.EXACTLY) {
mWidth = Size;
} else {
//由圖片決定寬度
int value = getPaddingLeft() + getPaddingRight() + bitmap.getWidth();
if (Mode == MeasureSpec.AT_MOST) {
//由圖片和Padding決定寬度,但是不能超過View的寬
mWidth = Math.min(value, Size);
}
}
return mWidth;
}
private int measuredHeight(int heightMeasureSpec) {
int Mode = MeasureSpec.getMode(heightMeasureSpec);
int Size = MeasureSpec.getSize(heightMeasureSpec);
if (Mode == MeasureSpec.EXACTLY) {
mHeight = Size;
} else {
//由圖片決定高度
int value = getPaddingTop() + getPaddingBottom() + bitmap.getHeight();
if (Mode == MeasureSpec.AT_MOST) {
//由圖片和Padding決定高度,但是不能超過View的高
mHeight = Math.min(value, Size);
}
}
return mHeight;
}
步驟二:獲取我們r(jià)es的圖片資源后,在ondraw方法中進(jìn)行繪制圓形圖片
//獲取res的圖片資源
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.concat(matrix);
//真實(shí)的半徑必須是View的寬高最小值
radius = Math.min(mWidth, mHeight);
//如果圖片本身寬高太大,進(jìn)行相應(yīng)的縮放
bitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
//畫圓形圖片
canvas.drawBitmap(createCircleImage(bitmap, radius), 0, 0, null);
matrix.reset();
}
private Bitmap createCircleImage(Bitmap source, int radius) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888);
//產(chǎn)生一個(gè)同樣大小的畫布
Canvas canvas = new Canvas(target);
//首先繪制圓形
canvas.drawCircle(radius / 2, radius / 2, radius / 2, paint);
//使用SRC_IN模式顯示后畫圖的交集處
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//繪制圖片,從(0,0)畫
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
步驟三:通過Handler發(fā)送Runnable來啟動(dòng)旋轉(zhuǎn)線程
//開始旋轉(zhuǎn)
mHandler.post(runnable);
[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
//-----------旋轉(zhuǎn)動(dòng)畫-----------
Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
matrix.postRotate(degrees++, radius / 2, radius / 2);
//重繪
invalidate();
mHandler.postDelayed(runnable, 50);
}
};
步驟四:在布局中使用我們的View
<com.handsome.cycle.MyCycleView android:layout_width="wrap_content" android:layout_height="wrap_content" />
下面是整個(gè)類的源碼
public class MyCycleView extends View {
//view的寬和高
int mHeight = 0;
int mWidth = 0;
//圓形圖片
Bitmap bitmap = null;
//圓形圖片的真實(shí)半徑
int radius = 0;
//旋轉(zhuǎn)動(dòng)畫的矩形
Matrix matrix = new Matrix();
//旋轉(zhuǎn)動(dòng)畫的角度
int degrees = 0;
//-----------旋轉(zhuǎn)動(dòng)畫-----------
Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
matrix.postRotate(degrees++, radius / 2, radius / 2);
//重繪
invalidate();
mHandler.postDelayed(runnable, 50);
}
};
public MyCycleView(Context context) {
super(context);
initView();
}
public MyCycleView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyCycleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
public void initView() {
//獲取res的圖片資源
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
//開始旋轉(zhuǎn)
mHandler.post(runnable);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//測量整個(gè)View的寬和高
mWidth = measuredWidth(widthMeasureSpec);
mHeight = measuredHeight(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
private int measuredWidth(int widthMeasureSpec) {
int Mode = MeasureSpec.getMode(widthMeasureSpec);
int Size = MeasureSpec.getSize(widthMeasureSpec);
if (Mode == MeasureSpec.EXACTLY) {
mWidth = Size;
} else {
//由圖片決定寬度
int value = getPaddingLeft() + getPaddingRight() + bitmap.getWidth();
if (Mode == MeasureSpec.AT_MOST) {
//由圖片和Padding決定寬度,但是不能超過View的寬
mWidth = Math.min(value, Size);
}
}
return mWidth;
}
private int measuredHeight(int heightMeasureSpec) {
int Mode = MeasureSpec.getMode(heightMeasureSpec);
int Size = MeasureSpec.getSize(heightMeasureSpec);
if (Mode == MeasureSpec.EXACTLY) {
mHeight = Size;
} else {
//由圖片決定高度
int value = getPaddingTop() + getPaddingBottom() + bitmap.getHeight();
if (Mode == MeasureSpec.AT_MOST) {
//由圖片和Padding決定高度,但是不能超過View的高
mHeight = Math.min(value, Size);
}
}
return mHeight;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.concat(matrix);
//真實(shí)的半徑必須是View的寬高最小值
radius = Math.min(mWidth, mHeight);
//如果圖片本身寬高太大,進(jìn)行相應(yīng)的縮放
bitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
//畫圓形圖片
canvas.drawBitmap(createCircleImage(bitmap, radius), 0, 0, null);
matrix.reset();
}
private Bitmap createCircleImage(Bitmap source, int radius) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888);
//產(chǎn)生一個(gè)同樣大小的畫布
Canvas canvas = new Canvas(target);
//首先繪制圓形
canvas.drawCircle(radius / 2, radius / 2, radius / 2, paint);
//使用SRC_IN模式顯示后畫圖的交集處
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//繪制圖片,從(0,0)畫
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義View實(shí)現(xiàn)QQ運(yùn)動(dòng)積分轉(zhuǎn)盤抽獎(jiǎng)功能
- Android 自定View實(shí)現(xiàn)仿QQ運(yùn)動(dòng)步數(shù)圓弧及動(dòng)畫效果
- Android自定義View仿微博運(yùn)動(dòng)積分動(dòng)畫效果
- Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放
- Android使用RotateImageView 旋轉(zhuǎn)ImageView
- Android UI設(shè)計(jì)系列之ImageView實(shí)現(xiàn)ProgressBar旋轉(zhuǎn)效果(1)
- Android自定義View葉子旋轉(zhuǎn)完整版(六)
- Android自定義View實(shí)現(xiàn)葉子飄動(dòng)旋轉(zhuǎn)效果(四)
- Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼
- Android自定義View圖片按Path運(yùn)動(dòng)和旋轉(zhuǎn)
相關(guān)文章
Android開發(fā)兩個(gè)activity之間傳值示例詳解
這篇文章主要為大家介紹了Android開發(fā)兩個(gè)activity之間傳值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Kotlin基礎(chǔ)學(xué)習(xí)之Deprecated與Suppress注解使用
這篇文章主要給大家介紹了關(guān)于Kotlin基礎(chǔ)學(xué)習(xí)之Deprecated與Suppress注解使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android畫圖并保存圖片的具體實(shí)現(xiàn)代碼
這篇文章介紹了在Android中畫圖并保存圖片的實(shí)例,以下是具體的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-07-07
Android 安全加密:數(shù)字簽名和數(shù)字證書詳解
本文主要介紹Android 安全加密數(shù)字簽名和數(shù)字證書的資料,這里整理詳細(xì)的資料及數(shù)字簽名和數(shù)字證書應(yīng)用詳解,有需要的小伙伴可以參考下2016-09-09
Android編程之SurfaceView實(shí)例詳解
這篇文章主要介紹了Android編程之SurfaceView用法,簡要分析了View和SurfaceView的區(qū)別,并結(jié)合實(shí)例形式分析了SurfaceView的具體使用步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
Android、iOS和Windows Phone中的推送技術(shù)詳解
這篇文章主要介紹了Android、iOS和Windows Phone中的推送技術(shù)詳解,推送技術(shù)的實(shí)現(xiàn)通常會(huì)使用服務(wù)端向客戶端推送消息的方式,也就是說客戶端通過用戶名、Key等ID注冊到服務(wù)端后,在服務(wù)端就可以將消息向所有活動(dòng)的客戶端發(fā)送,需要的朋友可以參考下2015-01-01
android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離
這篇文章主要為大家詳細(xì)介紹了android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

