android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable

先上效果图:



Layout文件:

<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="#99CC99"
    tools:context=".MainActivity" >
    
    
    <ImageView 
        android:id="@+id/img1"
        android:layout_centerInParent="true"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:src="@drawable/portrait"
        />
    
    <ImageView 
        android:id="@+id/img5"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/img1"
        android:layout_marginRight="20dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/portrait"
        />
    
    <ImageView 
        android:id="@+id/img6"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/img5"
        android:layout_marginRight="20dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="center"
        android:src="@drawable/portrait"
        />
    
    <ImageView 
        android:id="@+id/img4"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/img1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/portrait"
        />
    
     <ImageView 
        android:id="@+id/img7"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/img4"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/portrait"
        />
    
     
      <ImageView 
        android:id="@+id/img2"
        android:layout_marginBottom="20dp"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
      
        <ImageView 
        android:id="@+id/img3"
        android:layout_marginTop="20dp"
         android:layout_centerHorizontal="true"
        android:layout_below="@id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>


首先介绍两种把资源里的drawable转成bitmap的方式

第一种:

Bitmap bmp=BitmapFactory.decodeResource(this.getResources(), R.drawable.portrait);
第二种:

	Drawable d = this.getResources().getDrawable(R.drawable.portrait);
		Bitmap bmp = drawableToBitmap(d);
	public static Bitmap drawableToBitmap(Drawable drawable) {       
        Bitmap bitmap = Bitmap.createBitmap(
                                        drawable.getIntrinsicWidth(),
                                        drawable.getIntrinsicHeight(),
                                        Bitmap.Config.ARGB_8888
                                                        );

        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
}

把Bitmap加上圆角的方法:

private static RectF rrbRectf = new RectF();
	private static Path rrbRath = new Path();
	private static final int RRB_DEFAULT_SIZE = 10;
	
	public static Bitmap outputBmp(Context ctx, Bitmap src, boolean isCover) {
		Bitmap v = null;
		if (isCover) {
			v = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.v);
		}else{
			v = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.tv);
		}
		Bitmap bmp = null;
		int arcLength = RRB_DEFAULT_SIZE;
		if (src != null && arcLength > 0) {
			int width = src.getWidth();
			int height = src.getHeight();
			// Utils.loge("width:" + width + "height:" + height);
			Bitmap newBitmap = Bitmap.createBitmap(width, height,
					Bitmap.Config.ARGB_4444);
			Canvas canvas = new Canvas(newBitmap);
			rrbRath.reset();
			rrbRectf.set(0, 0, width, height);
			<strong>rrbRath.addRoundRect(rrbRectf, arcLength, arcLength,
					Path.Direction.CW);  //这里是加上圆角</strong>
			canvas.clipPath(rrbRath);
			canvas.drawBitmap(src, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));
			if (newBitmap != null && v != null) {
				int width1 = newBitmap.getWidth();
				int height1 = newBitmap.getHeight();
				int width2 = v.getWidth();
				int height2 = v.getHeight();
				bmp = Bitmap.createBitmap(width1 + (width2 / 2), height1
						+ (height2 / 2), Bitmap.Config.ARGB_4444);
				bmp.eraseColor(Color.TRANSPARENT);
				Canvas canvas2 = new Canvas(bmp);
				canvas2.drawBitmap(newBitmap, 0, 0, new Paint(
						Paint.ANTI_ALIAS_FLAG));
				<strong>canvas2.drawBitmap(v, width1 - (width2 / 2), height1
						- (height2 / 2), new Paint(Paint.ANTI_ALIAS_FLAG));  //这里是在图片右下角加上v</strong>
				newBitmap.recycle();
				v.recycle();
				return bmp;
			}
			src.recycle();
		}
		return bmp;

	}


scaletype的种类分为三类matrix(默认)、fit-X类、和center类。matrix就不多说。fit-X类中,
fitStart、fitCenter和fitEnd之间的都是根据需要使原图改变对ImgView进行适应,不剪裁,按matrix进行绘制,但它们
的区别在于基准不同。fitStart的基准为最上角的点(即matrix方式开始的点)fitCenter的基准点为中间的点
(matrix方式中可以使图片居中的点),而fitEnd的基准点为右下角的点(即matrix方式最后绘制点)。center类
中,center、centerCrop、centerInside都是以原图的几何中心点和ImagView的几何中心点为基准,且只绘制
ImagView大小的图像,不同的是是否保持原图大小和绘图的目标不同、采取的手段不同
CENTER /center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd   把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY  把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以通过以下步骤设置 Android ImageView圆角: 1. 创建一个 xml 文件,例如 "rounded_corner.xml",并将以下代码复制到文件中: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp" /> </shape> ``` 2. 在布局文件中,将 ImageView 的背景设置为刚才创建的 xml 文件: ``` <ImageView android:id="@+id/my_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_corner" android:src="@drawable/my_image" /> ``` 3. 如果需要在代码中设置圆角,可以使用以下代码: ``` ImageView imageView = findViewById(R.id.my_image_view); GradientDrawable drawable = (GradientDrawable) imageView.getBackground(); drawable.setCornerRadius(10f); ``` 其中,10f 是圆角的半径,可以根据需要进行调整。 ### 回答2: AndroidImageView是常用的一种控件,用于展示图片。在实际开发中,我们可能需要对ImageView进行一些特殊的样式设置,比如给一个ImageView设置圆角。下面是一些关于Android ImageView如何设置圆角的方法。 方法一:使用drawable实现圆角效果 我们可以将图片包裹在一个drawable里面,并使用该drawable设置ImageView的背景。因为可以对drawable进行裁剪,所以我们就可以通过在drawable里面设置圆角来实现ImageView圆角效果。 1.创建一个xml文件 drawable/round_corners.xml: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="20dp"/> <solid android:color="@color/yourColor"/> </shape> ``` 其中,corners属性设置的就是圆角的大小,solid属性设置的就是要填充的颜色。 2.使用该drawable设置ImageView的背景: ``` <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/round_corners" android:scaleType="centerCrop" /> ``` 需要注意的是,这种方法对ImageView的背景进行裁剪,而不是对展示的图片进行裁剪,因此可能会出现图片被拉伸的问题。 方法二:使用BitmapShader实现圆角效果 我们也可以通过创建一个圆形bitmap并将其设置为ImageView的背景来实现圆角效果。 1.创建一个自定义的CircleImageView类: ``` public class CircleImageView extends AppCompatImageView { private Paint mPaint; private BitmapShader mShader; private RectF mRectF; private float mRadius; public CircleImageView(Context context) { this(context, null); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mPaint = new Paint(); mPaint.setAntiAlias(true); } /** * 重写onSizeChanged,在ImageView的大小改变时重新计算圆形的半径 */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mRectF = new RectF(0, 0, w, h); mRadius = Math.min(w, h) / 2; mShader = new BitmapShader(drawableToBitmap(getDrawable()), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); invalidate(); } /** * 重写onDraw,绘制圆形bitmap */ @Override protected void onDraw(Canvas canvas) { mPaint.setShader(mShader); canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint); } /** * 将Drawable转换为Bitmap */ private Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof ColorDrawable) { Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawColor(((ColorDrawable) drawable).getColor()); return bitmap; } else { throw new IllegalArgumentException("Unsupported drawable type"); } } } ``` 2.在布局文件中使用自定义的CircleImageView: ``` <your.package.name.CircleImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/yourImage" android:scaleType="centerCrop" /> ``` 这种方法可以直接对图片进行裁剪,不会出现拉伸的问题。 综上所述,以上是两种常见的实现ImageView圆角的方法,读者可以根据实际需要选择合适的方法。 ### 回答3: 在Android中,我们可以使用ImageView来展示图片,有时候需要对图片进行圆角处理,这一过程涉及到几个步骤: 第一步,创建一个ShapeDrawable对象,用来实现圆角效果,代码如下: ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RoundRectShape(new float[]{ radius, radius, radius, radius, radius, radius, radius, radius }, null, null)); 其中,RoundRectShape的构造方法中传入一个float数组来指定每一个角的半径大小,这里都设置为radius。 第二步,使用BitmapDrawable图片转换为Bitmap,并设置给ShapeDrawableDrawable属性: Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap); shapeDrawable.setDrawable(bitmapDrawable); 第三步,将我们创建的ShapeDrawable对象与ImageView关联起来: ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setBackground(shapeDrawable); 这样,我们就完成了对ImageView进行圆角处理,具体实现过程如下: ``` ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RoundRectShape(new float[]{ radius, radius, radius, radius, radius, radius, radius, radius }, null, null)); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap); shapeDrawable.setDrawable(bitmapDrawable); ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setBackground(shapeDrawable); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值