中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

Android自定義View旋轉(zhuǎn)圓形圖片

一個(gè)自定義View,記錄一下思路和代碼以備以后使用。

成都創(chuàng)新互聯(lián)專注于浮山企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站定制開(kāi)發(fā)。浮山網(wǎng)站建設(shè)公司,為浮山等地區(qū)提供建站服務(wù)。全流程按需定制開(kāi)發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

思路:

1.首先要畫(huà)一個(gè)圓形圖片和一個(gè)圓形背景圖(通過(guò)自定義View);
2.自定義View基本步驟初始化屬性,測(cè)量寬高和中心點(diǎn),然后繪制圖片;
3.通過(guò)handler實(shí)現(xiàn)圖片的角度旋轉(zhuǎn).然后然后就慢慢擼.

效果圖:

Android自定義View旋轉(zhuǎn)圓形圖片 

1、廢話不多直接上代碼

public class MusicPlayerView extends View {
 private static final long ROTATE_DELAY = 5;//旋轉(zhuǎn)動(dòng)作時(shí)間
 private int mRotateDegrees;//旋轉(zhuǎn)的角度
 private Handler mRotate;
 private int mWidth;
 private int mHeight;
 private float mCenterX;
 private float mCenterY;
 private RectF rectF;
 private Bitmap mBitmapCover;
 private float mCoverScale;
 private BitmapShader mShader;
 private Paint paint;
 private boolean isRotating;
 private final Runnable mRunnableRotate = new Runnable() {
  @Override
  public void run() {
   if (isRotating) {
    updateCoverRotate();
    mRotate.postDelayed(mRunnableRotate, ROTATE_DELAY);
   }
  }
 };

 /**
  * 更新封面角度,重新繪制圖片
  */

 private void updateCoverRotate() {
  mRotateDegrees += 1;
  mRotateDegrees = mRotateDegrees % 360;
  postInvalidate();
 }

 /**
  * 判讀是否在旋轉(zhuǎn)
  * @return
  */
 public boolean isRotating() {
  return isRotating;
 }

 /**
  * 開(kāi)始旋轉(zhuǎn)圖片
  */
 public void start(){
  isRotating=true;
  mRotate.removeCallbacksAndMessages(null);
  mRotate.postDelayed(mRunnableRotate,ROTATE_DELAY);
  postInvalidate();
 }

 /**
  * 停止圖片旋轉(zhuǎn)
  */
 public void stop(){
  isRotating = false;
  postInvalidate();
 }

 /**
  * 通過(guò)本地圖片設(shè)置封面圖
  */
 public void setCoverDrawable(int coverDrawable) {
  Drawable drawable = getContext().getResources().getDrawable(coverDrawable);
  mBitmapCover = drawableToBitmap(drawable);
  createShader();
  postInvalidate();
 }
 /**
  * 網(wǎng)絡(luò)圖片加載使用Picasso圖片加載工具
  *
  * @param imageUrl
  */
 public void setCoverURL(String imageUrl) {
  Picasso.with(getContext()).load(imageUrl).into(target);
 }

 public MusicPlayerView(Context context) {
  super(context);
  init(context, null);
 }

 public MusicPlayerView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context, attrs);
 }

 public MusicPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init(context, attrs);
 }

 /**
  * 初始化View資源
  *
  * @param context
  * @param attrs
  */
 private void init(Context context, AttributeSet attrs) {
  setWillNotDraw(false);
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.play_view);
  Drawable mDrawableCover = a.getDrawable(R.styleable.play_view_cover);
  if (mDrawableCover != null) {
   mBitmapCover = drawableToBitmap(mDrawableCover);
  }
  a.recycle();
  mRotateDegrees = 0;
  //通過(guò)handler更新圖片角度
  mRotate = new Handler();
  rectF = new RectF();
 }

 /**
  * 測(cè)量寬高,設(shè)置中心點(diǎn)中心點(diǎn)位置,創(chuàng)建陰影
  *
  * @param widthMeasureSpec
  * @param heightMeasureSpec
  */

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  mWidth = MeasureSpec.getSize(widthMeasureSpec);
  mHeight = MeasureSpec.getSize(heightMeasureSpec);
  int minSide = Math.min(mWidth, mHeight); //取寬高最小值設(shè)置圖片寬高
  mWidth = minSide;
  mHeight = minSide;
  setMeasuredDimension(mWidth, mHeight);  //重新設(shè)置寬高
  //中心點(diǎn)位置
  mCenterX = mWidth / 2f;
  mCenterY = mHeight / 2f;
  //設(shè)置圖片顯示位置
  rectF.set(20.0f, 20.0f, mWidth - 20.0f, mHeight - 20.0f);
  createShader();
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  if (mShader == null) {
   return;
  }
  //畫(huà)封面圖片 判讀圖片的中心距離xy,算出邊角大小,然后畫(huà)圓
  float radius = mCenterX <= mCenterY ? mCenterX - 75.0f : mCenterY - 75.0f;
  canvas.rotate(mRotateDegrees, mCenterX, mCenterY);
  canvas.drawCircle(mCenterX, mCenterY, radius, paint);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:

    break;
  }
  return super.onTouchEvent(event);
 }


 private Target target = new Target() {
  @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
   mBitmapCover = bitmap;
   createShader();
   postInvalidate();
  }

  @Override
  public void onBitmapFailed(Drawable errorDrawable) {

  }

  @Override
  public void onPrepareLoad(Drawable placeHolderDrawable) {

  }
 };
 private int mCoverColor = Color.YELLOW;

 private void createShader() {
  if (mWidth == 0) {
   return;
  }
  if (mBitmapCover == null) { //如果封面為為創(chuàng)建默認(rèn)顏色
   mBitmapCover = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
   mBitmapCover.eraseColor(mCoverColor);
  }
  mCoverScale = ((float) mWidth) / (float) mBitmapCover.getWidth();
  //創(chuàng)建縮放后的bitmap
  mBitmapCover = Bitmap.createScaledBitmap(mBitmapCover,
    (int) (mBitmapCover.getWidth() * mCoverScale), (int) (mBitmapCover.getHeight() * mCoverScale), true);
  mShader = new BitmapShader(mBitmapCover, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(mShader);
 }

 /**
  * 將drawable轉(zhuǎn)換為位圖 為BitmapShader準(zhǔn)備
  *
  * @param drawable
  * @return
  */
 private Bitmap drawableToBitmap(Drawable drawable) {
  if (drawable instanceof BitmapDrawable) {
   return ((BitmapDrawable) drawable).getBitmap();
  }
  int width = drawable.getIntrinsicWidth();
  width = width > 0 ? width : 1;
  int height = drawable.getIntrinsicHeight();
  height = height > 0 ? height : 1;
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  drawable.draw(canvas);
  return bitmap;
 }
}

2.差點(diǎn)忘記一個(gè)attrs屬性

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="play_view">
  <attr name="cover" format="integer"/>
 </declare-styleable>
</resources>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享文章:Android自定義View旋轉(zhuǎn)圓形圖片
鏈接分享:http://www.rwnh.cn/article4/psjcoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站制作、自適應(yīng)網(wǎng)站標(biāo)簽優(yōu)化、網(wǎng)站排名、全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
微山县| 察哈| 大安市| 鸡西市| 龙州县| 呼和浩特市| 微山县| 万州区| 冀州市| 锦屏县| 夏津县| 澄迈县| 京山县| 加查县| 鹤山市| 邛崃市| 江门市| 龙海市| 广水市| 张北县| 麻栗坡县| 石林| 临泉县| 化州市| 渝北区| 武功县| 新昌县| 淮滨县| 海兴县| 东海县| 甘德县| 普兰店市| 监利县| 班玛县| 谢通门县| 和平区| 黄冈市| 邳州市| 苍溪县| 鄂伦春自治旗| 昭通市|