如何将角半径设置为imageview

时间:2016-08-06 09:09:23

标签: 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;
}

2 个答案:

答案 0 :(得分:1)

您只需要将此类放入Java包中

public class CustomCornerImageVew extends AppCompatImageView {

    private float radius = 18.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" />

答案 1 :(得分:0)

你可以用这种方式做XML

rails g controller Home index about

并且实际上你可以创建圆角位图并在ImageView中设置。

<stroke android:width="3dp"
        android:color="#ff000000"/>

<padding android:left="1dp"
         android:top="1dp"
         android:right="1dp"
         android:bottom="1dp"/> 

<corners android:radius="30px"/> 

对于Universal lazy loader,您也可以使用此wat。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), 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);
final float roundPx = 12;

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

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

return output;
}