Crop square image to circle - Programmatically

前端 未结 7 1454
余生分开走
余生分开走 2020-12-01 01:14

i was searching for past one day and i was not successful .

i get the image from API , and i download it to a bitmap file using the following code .

         


        
7条回答
  •  广开言路
    2020-12-01 01:51

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            DrawingView dv = new DrawingView(this);
            setContentView(dv);
        }
    
        class DrawingView extends View {
            Bitmap bitmap;
    
            public DrawingView(Context context) {
                super(context);
                bitmap = BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.glossy_overlay);
    
            }
    
            @Override
            public void onDraw(Canvas canvas) {
                Paint paint = new Paint();
                // paint.setColor(Color.CYAN);
                canvas.drawBitmap(getclip(), 30, 20, paint);
            }
    
            public Bitmap getclip() {
                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());
    
                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                // paint.setColor(color);
                canvas.drawCircle(bitmap.getWidth() / 2,
                        bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);
                return output;
            }
        }
    }
    

提交回复
热议问题