java - 如何设置 ImageView 的角半径

标签 java android

我想为 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/

相关文章:

java - Maven 在运行测试用例 Eclipselink 和 hsqldb 时挂起

java - 安卓 : getting an item from a textview to another textview?

Android RecyclerView 添加分页

android - 在没有麦克风的情况下在Android中录制音频流

java - NullPointerException - fragment

java - 列表 <Serialized> 到字符串

java - Swing 动态(自动)适配布局

android - 填充剩余空间 : LinearLayout and weight or RelativeLayout?

android - PopupWindow .showAsDropDown() 无法向左或向右移动(但向上和向下都可以)

android - 使用 Glide for Android,我如何从 Assets 和资源中加载图像?