Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 已调整大小的ImageView上的圆角_Java_Android_Android Layout_Imageview - Fatal编程技术网

Java 已调整大小的ImageView上的圆角

Java 已调整大小的ImageView上的圆角,java,android,android-layout,imageview,Java,Android,Android Layout,Imageview,我尝试在ImageView上圆角,如果layoutParams设置为WRAP\u CONTENT,则效果很好,但由于它们正在调整大小(因为它们设置为MATCH\u PARENT),我可能需要调整一些内容 画布/图像视图扩展类: import android.content.Context; import android.graphics.Canvas; import android.graphics.Path; import android.graphics.RectF; import andr

我尝试在ImageView上圆角,如果layoutParams设置为
WRAP\u CONTENT
,则效果很好,但由于它们正在调整大小(因为它们设置为
MATCH\u PARENT
),我可能需要调整一些内容

画布/图像视图扩展类:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CustomImageView extends ImageView {
    public static float radius = 18.0f;

    public CustomImageView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        //float radius = 36.0f;
        Path clipPath = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}
活动类别:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_gameplay);
   RelativeLayout gameplayRelativeLayoutImages = 
   (RelativeLayout)findViewById(R.id.gameplay_relative_layout_images);

   ImageView part1= new CustomImageView(this);
   part1.setImageResource(R.drawable.picture1);
   part1.setLayoutParams(new

   RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
   RelativeLayout.LayoutParams.MATCH_PARENT));
   gameplayRelativeLayoutImages.addView(part1);
 }

我不太确定你遇到的问题,但我已经用这个很酷的类创建了一段时间的圆形图像,我从来没有遇到过任何问题,试试吧

public class RoundedImageView extends android.support.v7.widget.AppCompatImageView {

public RoundedImageView(Context context) {
    super(context);
}

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

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

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
        float factor = smallest / radius;
        sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor),
                (int)(bmp.getHeight() / factor), false);
    } else {
        sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(radius, radius,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, radius, radius);

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(radius / 2 + 0.7f,
            radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}

}
使用方法:

ImageView mImgView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap= BitmapFactory.decodeResource(this.getResources(),
            R.drawable.some_image);

// bitmap image, radius
mImgView.setImageBitmap(RoundedImageView.getCroppedBitmap(bitmap, 360)); 

你能举一个问题的例子吗?@OrkunKoçyiğ它不能发布图像,因为我是新来的:x但基本上只有一个ImageView,它通过
MATCH_PARENT
属性调整大小。我怀疑是因为行
rectfrect=newrectf(0,0,this.getWidth(),this.getHeight())它采用实际的ImageView大小,而不是调整大小的更大的尺寸,因此它不能圆角。