How to define a triangle layout like this image in android?

前端 未结 3 515
梦毁少年i
梦毁少年i 2021-01-03 18:51

How to create a layout like shown in image?

3条回答
  •  情话喂你
    2021-01-03 19:40

    As stated above you can restructure the custom image class as the code below

    public class ProfileImageView extends AppCompatImageView{
    public ProfileImageView(Context context) {
        super(context);
    }
    
    public ProfileImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    public ProfileImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    
    
    @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 = getRoundedCroppedBitmap(bitmap, w,this.getLayoutParams().width,this.getLayoutParams().height);
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    
    }
    
    
    
    public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius, int dimension_w, int dimension_h) {
        Bitmap finalBitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                    false);
        else
            finalBitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
    
        Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());
    
    
        Log.e("Profile",""+dimension_w);
    
        Point point1_draw = new Point(0, 0);
        Point point2_draw = new Point(dimension_w, 0);
        Point point3_draw = new Point(dimension_w, (dimension_h/2));
        Point point4_draw = new Point(0, dimension_h);
    
    
        Path path = new Path();
        path.moveTo(point1_draw.x, point1_draw.y);
        path.lineTo(point2_draw.x, point2_draw.y);
        path.lineTo(point3_draw.x, point3_draw.y);
        path.lineTo(point4_draw.x, point4_draw.y);
        path.lineTo(point1_draw.x, point1_draw.y);
        path.close();
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawPath(path, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(finalBitmap, rect, rect, paint);
    
        return output;
    }
    

    }

    Sample screen shot

提交回复
热议问题