簡體   English   中英

使用Canvas和canvas.scale(Scale,Scale)的圖像質量問題;

[英]Image Quality problems using Canvas and canvas.scale(Scale, Scale);

我在使用Canvas和canvas.scale(Scale,Scale)時出現圖像質量問題; 它們看起來完全像以下內容:

android:在運行時調整大小的圖像的質量

我相信我在重新調整位圖大小時已經閱讀了有關圖像質量問題的所有文章,但是在使用Canvas比例(浮動比例)進行縮放時似乎沒有幫助。

我嘗試了許多不同的選項,如圖像質量帖子所建議。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;//I thought this would do it
CurrentPicture = BitmapFactory.decodeFile(path, options);//Also tried decodeStream()


PicturePaint = new Paint();
//PicturePaint = new Paint(Paint.FILTER_BITMAP_FLAG); //I also tried this
//PicturePaint = new Paint(Paint.ANTI_ALIAS_FLAG);  //I also tried this
canvas.scale(Scale, Scale);
canvas.drawBitmap(CurrentPicture, 0, 0, PicturePaint);

我相信這是實現目標的最后障礙。 我很擔心,因為如果無法解決圖像質量問題,我會遇到麻煩。 任何幫助表示贊賞。

謝謝!

系統不允許我發布圖片,因此下面是一個鏈接。

圖片樣本

我不是我的答案是否正確,我是否有適用於畫布的代碼。 我希望這對您有幫助。

public class FotosurpriseActivity extends Activity {
    /** Called when the activity is first created. */
    Bitmap overlay;
    Paint pTouch;
    int X = -100;
    int Y = -100;
    Canvas c2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
        Bitmap mBitmapover = BitmapFactory.decodeResource(getResources(), R.drawable.ss);
        overlay = BitmapFactory.decodeResource(getResources(), R.drawable.ss).copy(Config.ARGB_8888, true);
        c2 = new Canvas(overlay);

        pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
        // pTouch.setXfermode(new PorterDuffXfermode(Mode.TARGET);
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
        setContentView(new BitMapView(this, mBitmap, mBitmapover));
    }

    class BitMapView extends View {
        Bitmap mBitmap = null;
        Bitmap mBitmapover = null;

        public BitMapView(Context context, Bitmap bm, Bitmap bmover) {
            super(context);
            mBitmap = bm;
            mBitmapover = bmover;
        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {

            switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN: {

                X = (int) ev.getX();
                Y = (int) ev.getY();
                invalidate();

                break;
            }

            case MotionEvent.ACTION_MOVE: {

                X = (int) ev.getX();
                Y = (int) ev.getY();
                invalidate();
                break;

            }

            case MotionEvent.ACTION_UP:

                break;

            }
            return true;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // called when view is drawn
            Paint paint = new Paint();
            paint.setFilterBitmap(true);
            // The image will be scaled so it will fill the width, and the
            // height will preserve the image’s aspect ration
            /*
             * double aspectRatio = ((double) mBitmap.getWidth()) /
             * mBitmap.getHeight(); Rect dest = new Rect(0, 0,
             * this.getWidth(),(int) (this.getHeight() / aspectRatio)); double
             * aspectRatio2 = ((double) mBitmapover.getWidth()) /
             * mBitmapover.getHeight(); Rect dest2 = new Rect(0, 0,
             * this.getWidth(),(int) (this.getHeight() / aspectRatio2));
             * canvas.drawBitmap(mBitmap, null, dest, paint);
             * canvas.drawBitmap(mBitmapover, null, dest2, paint);
             */

            // draw background
            canvas.drawBitmap(mBitmap, 0, 0, null);
            // copy the default overlay into temporary overlay and punch a hole
            // in it
            c2.drawBitmap(mBitmapover, 0, 0, null); // exclude this line to show
                                                    // all as you draw
            c2.drawCircle(X, Y, 80, pTouch);
            // draw the overlay over the background
            canvas.drawBitmap(overlay, 0, 0, null);
        }
    }

}

我嘗試了很多代碼。 我只是將其添加到清單中,所以我得到了質量最好的圖像。

<uses-sdk android:minSdkVersion="9" />

我認為問題在於畫布是在低分辨率下創建的。 如果您嘗試在屏幕上打印設備的分辨率,您會發現它是錯誤的。 隨着清單中的添加,分辨率發生變化。

您是否有一個截圖可以顯示您所談論的質量問題? 如果在Paint上啟用了過濾,則應該可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM