Why isn't my image scaling correctly?

Problems with Canvas, OpenGL, etc...

Why isn't my image scaling correctly?

Postby graphitex » Mon Jun 13, 2011 6:31 pm

I'm trying to have an image scale to a certain size depending on the horizontal size sent to an update function, but the following code doesnt seem to size the image correctly.

GlassesView.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GlassesView extends View {
  2.  
  3.     private Paint paint;
  4.     private BitmapFactory.Options options;
  5.     private Bitmap bitmapOrg;
  6.     private int glassesWidth;
  7.     private int glassesHeight;
  8.  
  9.     public int glassesX;
  10.     public int glassesY;
  11.     public float glassesSizeX;
  12.     public float glassesSizeY;
  13.     private boolean drawGlasses;
  14.  
  15.     public GlassesView(Context context) {
  16.         super(context);
  17.         paint = new Paint();
  18.         paint.setDither(false);
  19.         paint.setAntiAlias(false);
  20.  
  21.         options = new BitmapFactory.Options();
  22.         options.inDither = false;
  23.         options.inScaled = false;
  24.  
  25.         Bitmap bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.micro_glasses, options);
  26.         bitmapOrg = Bitmap.createScaledBitmap(bitmapTemp, 32, 5, false);
  27.  
  28.         glassesWidth = bitmapOrg.getWidth();
  29.         glassesHeight = bitmapOrg.getHeight();
  30.  
  31.         drawGlasses = false;
  32.     }
  33.  
  34.     @Override
  35.     protected void onDraw(Canvas canvas) {
  36.         if(drawGlasses) {
  37.             Matrix matrix = new Matrix();
  38.             matrix.postScale(glassesSizeX, glassesSizeY);
  39.  
  40.             Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, glassesWidth, glassesHeight, matrix, false);
  41.  
  42.             int newWidth = resizedBitmap.getWidth();
  43.             int newHeight = resizedBitmap.getHeight();
  44.             Paint bluey = new Paint();
  45.             bluey.setColor(Color.BLUE);
  46.             canvas.drawRect(new Rect(glassesX, glassesY, glassesX+newWidth, glassesY+newHeight), bluey);
  47.  
  48.             canvas.drawBitmap(resizedBitmap, glassesX, glassesY, paint);
  49.         }
  50.     }
  51.  
  52.     public void updateGlasses(int x, int y, float xsize, float ysize) {
  53.         drawGlasses = true;
  54.         glassesX = x;
  55.         glassesY = y;
  56.         glassesSizeX = (float) xsize / (float) glassesWidth;
  57.         glassesSizeY = glassesSizeX;
  58.     }
  59.  
  60. }
  61.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


MainScreen.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MainScreen extends Activity {
  2.         LinearLayout mLinearLayout;
  3.         GlassesView mGlasses;
  4.        
  5.         int startx;
  6.         int starty;
  7.         int     endx;
  8.  
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         mLinearLayout = new LinearLayout(this);
  12.        
  13.         mLinearLayout.setBackgroundColor(Color.argb(255, 255, 255, 255));
  14.        
  15.         mGlasses = new GlassesView(this);
  16.        
  17.         mGlasses.drawGlasses(80, 80, 32.0f, 32.0f);
  18.        
  19.         mLinearLayout.addView(mGlasses, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  20.        
  21.         setContentView(mLinearLayout);
  22.        
  23.        
  24.     }
  25.  
  26.         @Override
  27.         public boolean onTouchEvent(MotionEvent event) {
  28.                 int action = event.getAction();
  29.                 if(action == MotionEvent.ACTION_DOWN) {
  30.                         startx = (int) event.getX();
  31.                         starty = (int) event.getY()- 40;
  32.                 }
  33.                 if(action == MotionEvent.ACTION_UP) {
  34.                         endx = (int) event.getX();
  35.                         float scale = endx-startx;
  36.                         mGlasses.drawGlasses(startx, starty, scale, scale);
  37.                         mGlasses.invalidate();
  38.                 }
  39.                 return super.onTouchEvent(event);
  40.         }
  41.    
  42.    
  43. }
  44.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The result: Image (The blue rectangle being the bounding box of the image's intended size)

At which part am I going wrong?
Thanks in advance :D
graphitex
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jun 13, 2011 6:24 pm

Top

Re: Why isn't my image scaling correctly?

Postby ng.kishore » Mon Aug 01, 2011 5:35 am

Hi graphitex,

In your GlassView class when creating a bitmap you are scaling the image to 30 x 5, and you are perming changes on this image.

check below code
Bitmap bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
bitmapOrg = Bitmap.createScaledBitmap(bitmapTemp, 32, 5, false);


insted of passing fixed values for bitmapOrg, pass the original width and height of the image, as below
Bitmap bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
bitmapOrg = Bitmap.createScaledBitmap(bitmapTemp, bitmapTemp.getWidth(), bitmapTemp.getHeight(), false);
N G Kishore.
ng.kishore
Developer
Developer
 
Posts: 33
Joined: Wed Apr 28, 2010 11:07 am
Location: Hyderabad

Re: Why isn't my image scaling correctly?

Postby eee » Mon Aug 01, 2011 1:32 pm

Scaling code scale to 200x50 example:
ImageView im = (ImageView)findViewById(R.id.imageView1);

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
Bitmap bs = Bitmap.createScaledBitmap(bm, 200, 50, true);
im.setImageBitmap(bs);
eee
Freshman
Freshman
 
Posts: 7
Joined: Wed Jul 27, 2011 6:46 pm

Re: Why isn't my image scaling correctly?

Postby eee » Mon Aug 01, 2011 1:34 pm

I have a problem in cropping an image. The user must point to the section he wishes to crop and the rest of the image will be cut. can anyone help me? its urgent
eee
Freshman
Freshman
 
Posts: 7
Joined: Wed Jul 27, 2011 6:46 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests