[英]Java/Android rounded drawable/bitmap - 4 diffrent radiuses and size
我進行了搜索,但沒有找到任何有助於解決問題的線索:
我有一個ImageView帶有可繪制的圖層,該圖層可顯示該視圖的邊框半徑。 我還設置了jpg / png文件,該文件應顯示其背景(並且應該更像CSS,等同於類似於'background-image:url(....)'
),
我的主要問題是,當jpg / png文件較小時,存在先將其弄角然后拉伸的問題,結果圖像的原因看起來更像是圓形,而不是像帶圓角的拉伸矩形。
重要說明:我有4個不同的角半徑(0到1000之間的任何數字),例如腹板半徑-左上角,半徑-右上角等。
任何人都遇到過這樣的問題,或者知道更好的實現方法嗎?
我試圖添加使用自定義可繪制對象:
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);
}
}
我的with層列表顯示如下:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#ff00ff"></solid>
<corners android:topLeftRadius="50px" android:topRightRadius="120px"
android:bottomLeftRadius="90px"
android:bottomRightRadius="40px"></corners>
<padding android:top="20px" android:left="20px" android:right="20px" android:bottom="20px"></padding>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#f0000f"></solid>
<corners android:topLeftRadius="50px" android:topRightRadius="120px"
android:bottomLeftRadius="90px"
android:bottomRightRadius="40px"></corners>
<padding android:top="1px" android:bottom="1px" android:left="1px" android:right="1px"></padding>
</shape>
</item>
</layer-list>
另外,我的layout.xml呈現為:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/sk"
android:layout_width="320px"
android:layout_height="380px"
android:background="@drawable/skdrawable"
android:src="@drawable/ski2"
android:scaleType="fitCenter"
/>
</LinearLayout>
在我的活動中也應該執行任務的代碼:
ImageView view = (ImageView) findViewById(R.id.sk);
BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.ski2);
view.setImageDrawable(new RoundCornersBitmap(d.getBitmap(),view));
我將不勝感激。
我發現在imageview中使用額外路徑的解決方法:
Rect outRect = new Rect();
getDrawingRect(outRect);
mPath.addRoundRect(new RectF(outRect.left,outRect.top, width,height), floats_array, Path.Direction.CW);
並將其剪切到onDraw中:
canvas.clipPath(mPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.