IDE预览效果
手机截屏
100行代码实现圆形ImageView
package com.example.androidtest; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.Bitmap.Config; import android.util.AttributeSet; import android.widget.ImageView; public class CircleView extends ImageView { public CircleView(Context context) { super(context); } public CircleView(Context context, AttributeSet attrs) { super(context, attrs); } public CircleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 布局预览时会调用该方法 */ @Override public void setImageDrawable(Drawable drawable) { Bitmap bitmap1 = getBitmapFromDrawable(drawable);//获取到Bitmap //创建同样大小的Bitmap作为画布 Bitmap bitmap2 = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), Config.ARGB_8888); //画在bitmap2上 Canvas canvas = new Canvas(bitmap2); Paint paint = new Paint(); paint.setAntiAlias(true); //找到短的边作为圆的直径 int r; if (bitmap2.getHeight() > bitmap2.getWidth()) { r = bitmap2.getWidth() / 2; } else { r = bitmap2.getHeight() / 2; } //在bitmap2上画圆 canvas.drawCircle(bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, r, paint); //设置为SRC_IN,即显示重叠部分中后画的部分。 先画的部分为DST,后画的为SRC paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); //把原来的bitmap1画到bitmap2上 canvas.drawBitmap(bitmap1, 0, 0, paint); //上面执行完毕后bitmap2就是所要的圆形图片了 super.setImageDrawable(new BitmapDrawable(bitmap2)); } @Override public void setImageResource(int resId) { setImageDrawable(getResources().getDrawable(resId)); } /** * 获取Drawable对应的Bitmap * @param drawable * @return */ private Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap; if (drawable instanceof ColorDrawable) { bitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888); } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } } }
布局
<ScrollView 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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#888" android:orientation="vertical" > <com.example.androidtest.CircleView android:id="@+id/civ" android:scaleType="centerCrop" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/pic" /> <ImageView android:layout_marginTop="10dp" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:background="#080" android:src="@drawable/pic" /> <com.example.androidtest.CircleView android:id="@+id/civ2" android:scaleType="centerCrop" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/iii" /> <ImageView android:layout_marginTop="10dp" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:background="#080" android:src="@drawable/iii" /> <com.example.androidtest.CircleView android:id="@+id/civ3" android:scaleType="centerCrop" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/pic" /> <ImageView android:layout_marginTop="10dp" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:background="#080" android:src="@drawable/pic" /> </LinearLayout> </ScrollView>
package com.example.androidtest; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.graphics.Bitmap.Config; import android.graphics.PorterDuff.Mode; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CircleView civ=(CircleView) findViewById(R.id.civ); Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.pic); civ.setImageBitmap(bitmap1); CircleView civ2=(CircleView) findViewById(R.id.civ2); civ2.setImageResource(R.drawable.iii); CircleView civ3=(CircleView) findViewById(R.id.civ3); civ3.setImageDrawable(getResources().getDrawable(R.drawable.iii));; } }