android与java的绘图,Java / Android圆形绘图/位图 - 4个不同的半径和大小(Java/Android rounded drawable/bitmap - 4 diffrent...

博主遇到一个问题,当ImageView设置为具有不同角落半径的层叠Drawable时,小尺寸的jpg/png背景图片首先会被裁剪成圆角,然后拉伸,导致看起来像圆形而非带有圆角的矩形。尝试了自定义Drawable类RoundCornersBitmap来实现,但效果不佳。寻求解决方案,期望能保持图片原有形状并应用圆角效果。
摘要由CSDN通过智能技术生成

I searched but not found any clue to help my problem:

I have an ImageView with some layer drawable that presents border radiuses to that view. Also I set jpg/png file that should present its background(and should be more like css equivalent to code shortly like 'background-image:url(....)'),

My main issue is that when jpg/png file is small theres a problem that it is first cornered, and then stretched, and cause of result image looks more like circle, not like stretched rectangle with rounded corners.

Important note:I have 4 different corner-radiuses(any number from 0 to 1000), like in web radius-top-left,radius-top-right, etc.

Anyone had problem like that or know better way of implementing this?

I tried to add use custom drawable:

public class RoundCornersBitmap extends PaintDrawable {

private Bitmap mBitmap;

private final Paint mPaint;

private final BitmapShader mShader;

private final ImageView mView;

public RoundCornersBitmap(Bitmap bmp, ImageView view) {

mBitmap = bmp;

mPaint = new Paint();

mShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

mPaint.setStyle(Paint.Style.FILL);

mPaint.setAntiAlias(true);

mPaint.setColor(Color.BLACK);

mPaint.setShader(mShader);

mPaint.setFlags(Paint.FILTER_BITMAP_FLAG);

mView = view;

//example corners

this.setCornerRadii(new float[]{50, 50, 120, 120, 90, 90, 40, 40});

}

@Override

public int getIntrinsicWidth() {

return mBitmap.getWidth();

}

@Override

public int getIntrinsicHeight() {

return mBitmap.getHeight();

}

@Override

public Paint getPaint() {

return mPaint;

}

@Override

public void draw(Canvas canvas) {

getShape().draw(canvas, mPaint);

}

}

and my with layer list presents as folowing:

android:bottomLeftRadius="90px"

android:bottomRightRadius="40px">

android:bottomLeftRadius="90px"

android:bottomRightRadius="40px">

addinationaly my layout.xml presents like that:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@+id/sk"

android:layout_width="320px"

android:layout_height="380px"

android:background="@drawable/skdrawable"

android:src="@drawable/ski2"

android:scaleType="fitCenter"

/>

also code in my activity that should do the task:

ImageView view = (ImageView) findViewById(R.id.sk);

BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.ski2);

view.setImageDrawable(new RoundCornersBitmap(d.getBitmap(),view));

I'd appreciate any help .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值