Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:01:19+00:00 2026-06-14T04:01:19+00:00

I have a relative layout on which i add a view. this is the

  • 0

I have a relative layout on which i add a view. this is the code in the activity:

paint = (RelativeLayout) findViewById(R.id.paint);
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            myView = new CustomImage(getBaseContext());
            paint.addView(myView);
            String imagePath = (String) getIntent().getExtras().get("selectedImagePath");
            LogService.log("PaintActivity", "Imagepath: " + imagePath);
            //                Drawable background = BitmapDrawable.createFromPath(imagePath);
            //                bitmap = ((BitmapDrawable) background).getBitmap();
            try {
                fis = new FileInputStream(imagePath);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                mBitmap = loadBitmap(fis.getFD());
                bitmap = getResizedBitmap(mBitmap, 412*2, 1024*2);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                LogService.log("PaintActivity", "Bitmap = " + mBitmap);
                myView.setBitmap(bitmap);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFF000000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(50);
            myView.setPaint(mPaint);

        }
    }, 50);

This is my View:

public class CustomImage extends View {

private boolean hasResized = false;

private Bitmap bitmap, bitmap2;

public Context context;

Paint paint;

private Canvas canvas;

public String text = null;

private Paint mBitmapPaint, paintText;

private Path mPath = new Path();

private float mX, mY, pX, pY, tX, tY;

private static final float TOUCH_TOLERANCE = 4;

private float screenDensity;

int height, width;

private Bitmap mBitmap;

private RandomAccessFile randomAccessFile;

private int bh;

private int bw;

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

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

public CustomImage(Context context) {
    super(context);
    init(context);
}

private void init(Context context) {
    this.context = context;
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  }

public void setImg(Context context, Canvas canvas, Bitmap bitmapw, int width, int height) {

    bw = bitmapw.getWidth();
    bh = bitmapw.getHeight();
    float scaleWidth = ((float) width)/ bw;
    float scaleHeight = ((float) height)/ bh;
    System.out.println("CustomImage.setImg()");
    bitmap = bitmapw;

}

public void setPaint(Paint paint) {
    this.paint = paint;
    LogService.log("in setPaint", "paint = " + paint);
}

public void setBitmap(Bitmap bitmap) throws Exception {
    File file = new File("/mnt/sdcard/sample/temp.txt");
    file.getParentFile().mkdirs();
    randomAccessFile = new RandomAccessFile(file, "rw");
    int bWidth = bitmap.getWidth();
    int bHeight = bitmap.getHeight();
    FileChannel channel = randomAccessFile.getChannel();
    MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, bWidth*bHeight*4);
    bitmap.copyPixelsToBuffer(map);
    bitmap.recycle();
    this.bitmap = Bitmap.createBitmap(bWidth, bHeight, Config.ARGB_8888);
    map.position(0);
    this.bitmap.copyPixelsFromBuffer(map);
    channel.close();
    randomAccessFile.close();
    //        this.bitmap = bitmap;


    // canvas = new Canvas();
}

public void setBitmap2(Bitmap bitmap) {
    bitmap2 = bitmap;
    invalidate();
}

public void getText(String text) {
    this.text = text;
    paintText = new Paint();
    paintText.setColor(0xFFFFFFFF);
    paintText.setStrokeWidth(10);
    paintText.setTextSize(20);
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);
    if (PaintActivity.isPic == 1) {
        if (bitmap2 != null) {
            canvas.drawBitmap(bitmap2, mX, mY, mBitmapPaint);
            pX = mX;
            pY = mY;
        }
        if(text != null){
            canvas.drawText(text, tX, tY, paintText);
        }
    } else if (PaintActivity.isPic == 2) {
        if(text != null){
            canvas.drawText(text, mX, mY, paintText);
            tX = mX;
            tY = mY;
        }
        if (bitmap2 != null) {
            canvas.drawBitmap(bitmap2, pX, pY, mBitmapPaint);
        }
    }else{
        canvas.drawPath(mPath, paint);
        if (bitmap2 != null) {
            canvas.drawBitmap(bitmap2, pX, pY, mBitmapPaint);
        }
        if(text != null){
            canvas.drawText(text, tX, tY, paintText);
        }
    }
}

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    canvas.drawPoint(x, y, paint);
    mX = x;
    mY = y;

}
private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    mPath.moveTo(mX, mY);
    canvas.drawPath(mPath, paint);
    mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);

        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    System.out.println("CustomImage.onSizeChanged()");
    super.onSizeChanged(w, h, oldw, oldh);
    if ((w != 0) && (h != 0)) {
        if (!hasResized) {
            hasResized = true;
            renderImage();
        }
    }
}

public void renderImage() {
    System.out.println("======in renderimage=======w " + getMeasuredWidth() + "h " + getMeasuredHeight());
    width = getMeasuredWidth();
    height = getMeasuredHeight();
    canvas = new Canvas(bitmap);
    setImg(context, canvas, bitmap, width, height);
}
}

As you can see, I have a resizing function in the activity:

  public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

}

Now if i comment this function out of the code, the image looks normal, but not scaled. If i leave it there, then i get an ugly image, but it is resized. Example:
normal not resized: http://imgur.com/BD5Pp,YWDTi
altered but resized pic: http://imgur.com/BD5Pp,YWDTi#1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:01:21+00:00Added an answer on June 14, 2026 at 4:01 am

    As OP stated answering my question, the bitmap is converted to RGB_565, which is a known bug in createBitmap() – #16211, fixed in Android 3.0. It affects createScaledBitmap() as well. The bitmap wouldn’t look distorted if it remained ARGB_8888.

    As a workaround you have to create target bitmap manually (which gives you full control of the pixel format), then create canvas attached to this bitmap and paint a scaled version of the original bitmap on it. Here’s a sample from my project:

    public class BitmapUtils {
    
        private static Matrix matrix = new Matrix();
        private static Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    
        public static final Bitmap resizeBitmap(Bitmap bitmap, float scale, Bitmap.Config targetConfig) {
                int srcWidth = bitmap.getWidth();
                int srcHeight = bitmap.getHeight();
    
                int newWidth = (int) (srcWidth * scale);
                int newHeight = (int) (srcHeight * scale);
    
                float sx = newWidth / (float) srcWidth;
                float sy = newHeight / (float) srcHeight;
                matrix.setScale(sx, sy);
    
                Bitmap target = Bitmap.createBitmap(newWidth, newHeight, targetConfig);
                Canvas c = new Canvas(target);
                c.drawBitmap(bitmap, matrix, paint);
                return target;
        }
    }
    

    Note: this code is not reentrant, due to global matrix instance. Also, it takes scale as parameter, but adapting it to your needs (new width/height as parameters) is trivial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Activity i have: DrawView which is a Custom Relative Layout. This RelativeLayout
I have this layout that works correctly, a relative layout with a text view
I have designed the screen using this relative layout. <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a ViewFlipper in which I want to add Views (Relative Layout with
I have created following layout.which has two relative layouts and one scrollview ,following code
I have imageView inside a relative layout. And when i create the activity i
I have to add a custom layout in a relative layout on a button
http://dl.dropbox.com/u/24856/android_issue.PNG i have a text view on top running into the relative layout with
I have a Relative Layout. Which has 2 buttons, side by side and it
I have created sliding drawer for my relative layout which includes some buttons. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.