Bitmap not drawn anti-aliased

后端 未结 5 1187
遥遥无期
遥遥无期 2020-12-05 00:50

I have a custom View that always draws a Bitmap at a certain rotation. I overwrite the onDraw method, rotate the Canvas a

5条回答
  •  忘掉有多难
    2020-12-05 01:30

    I tested the previous suggestions and they do work for me. Maybe the issues is in your bitmap ? Maybe in your device? Here is a code that creates the bitmap inline. Check if this example works for you.

    public class BitmapRotateActivity extends Activity { 
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
      }
      class MyView extends View {
    
        Bitmap bitmap;
        public MyView(Context context) {
          super(context);
    
          bitmap = Bitmap.createBitmap(160, 160, Bitmap.Config.RGB_565);
          Paint p = new Paint();
          p.setColor(Color.RED);
          p.setStyle(Paint.Style.FILL);
          Canvas c = new Canvas(bitmap);
          c.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), p);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
          Paint pbg = new Paint();
          pbg.setStyle(Paint.Style.FILL);
          pbg.setColor(0xffe0e0ff);
          canvas.drawRect(0, 0, getWidth(), getHeight(), pbg);
    
          Paint p = new Paint();
          p.setAntiAlias(true);
          p.setFilterBitmap(true);
    
          canvas.save();
          canvas.rotate(3F, getWidth() / 2, getHeight() / 2);
          canvas.drawBitmap(bitmap, 30, 30, p);
          canvas.restore();
        }
      }
    }
    

提交回复
热议问题