gpt4 book ai didi

java - 如何设置 ImageView 的角半径

转载 作者:行者123 更新时间:2023-12-01 21:23:11 26 4
gpt4 key购买 nike

我想为 imageview 设置角半径并控制搜索栏上的半径。随着搜索栏的前进,角半径应该增加,反之亦然。目前,我在增加搜索栏上获得角半径。但当搜索栏移回时,它不会将 imageview 设置为其原始状态。

    cornerRadius.setMax(100);
cornerRadius.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

public void onStopTrackingTouch(SeekBar seekBar) {
}

public void onStartTrackingTouch(SeekBar seekBar) {
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

radius= progress;

for (int i = 0; i < IMGS.size(); i++) {
final PhotoView child = IMGS.get(i);
Bitmap viewCapture = null;

child.setDrawingCacheEnabled(true);

viewCapture = Bitmap.createBitmap(child.getDrawingCache());

child.setDrawingCacheEnabled(false);
child.setImageBitmap(getRoundedCornerBitmap(viewCapture,radius));
});


public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);


paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

最佳答案

将此类放入您的 java 包中

public class CustomCornerImageVew extends AppCompatImageView {

private float radius = 20.0f;
private Path path;
private RectF rect;

public CustomCornerImageVew(Context context) {
super(context);
init();
}

public CustomCornerImageVew(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomCornerImageVew(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
path = new Path();

}

@Override
protected void onDraw(Canvas canvas) {
rect = new RectF(0, 0, this.getWidth(), this.getHeight());
path.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
}

并在 xml 文件中声明 imageview,如下所示

 <YourPackageName.CustomCornerImageVew
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/mes" />

关于java - 如何设置 ImageView 的角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com