一个自定义view,记录一下思路和代码以备以后使用。
思路:
1.首先要画一个圆形图片和一个圆形背景图(通过自定义view);
2.自定义view基本步骤初始化属性,测量宽高和中心点,然后绘制图片;
3.通过handler实现图片的角度旋转.然后然后就慢慢撸.
效果图:
1、废话不多直接上代码
public class MusicPlayerVIEw extends VIEw { private static final long ROTATE_DELAY = 5;//旋转动作时间 private int mRotatedegrees;//旋转的角度 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(); } /** * 判读是否在旋转 * @return */ public boolean isRotating() { return isRotating; } /** * 开始旋转图片 */ public voID start(){ isRotating=true; mRotate.removeCallbacksAndMessages(null); mRotate.postDelayed(mRunnableRotate,ROTATE_DELAY); postInvalIDate(); } /** * 停止图片旋转 */ public voID stop(){ isRotating = false; postInvalIDate(); } /** * 通过本地图片设置封面图 */ public voID setCoverDrawable(int coverDrawable) { Drawable drawable = getContext().getResources().getDrawable(coverDrawable); mBitmapCover = drawabletoBitmap(drawable); createShader(); postInvalIDate(); } /** * 网络图片加载使用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; //通过handler更新图片角度 mRotate = new Handler(); rectF = new RectF(); } /** * 测量宽高,设置中心点中心点位置,创建阴影 * * @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); //取宽高最小值设置图片宽高 mWIDth = minSIDe; mHeight = minSIDe; setMeasuredDimension(mWIDth,mHeight); //重新设置宽高 //中心点位置 mCenterX = mWIDth / 2f; mCenterY = mHeight / 2f; //设置图片显示位置 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; } //画封面图片 判读图片的中心距离xy,算出边角大小,然后画圆 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) { //如果封面为为创建默认颜色 mBitmapCover = Bitmap.createBitmap(mWIDth,mHeight,Bitmap.Config.ARGB_8888); mBitmapCover.erasecolor(mCovercolor); } mCoverScale = ((float) mWIDth) / (float) mBitmapCover.getWIDth(); //创建缩放后的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转换为位图 为BitmapShader准备 * * @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,canvas.getWIDth(),canvas.getHeight()); drawable.draw(canvas); return bitmap; }}
2.差点忘记一个attrs属性
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="play_vIEw"> <attr name="cover" format="integer"/> </declare-styleable></resources>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:Android使用Matrix旋转图片模拟碟片加载过程Android中利用matrix 控制图片的旋转、缩放、移动Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍Android 图片缩放与旋转的实现详解Android实现图片反转、翻转、旋转、放大和缩小Android UI之ImageView实现图片旋转和缩放android图片处理之让图片一直匀速旋转Android部分手机拍照后获取的图片被旋转问题的解决方法基于Android 实现图片平移、缩放、旋转同时进行解决android有的手机拍照后上传图片被旋转的问题 总结以上是内存溢出为你收集整理的Android自定义View旋转圆形图片全部内容,希望文章能够帮你解决Android自定义View旋转圆形图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)