Updated: Rotate a bitmap that exceeds screen size - android

My image is bigger, but just a part of it have to be displayed on the screen. I have done this. My problem is that, the image has to be rotated by finger. The code below works as it should on a small image. But on my image it has a wrong behave, repositioning the image up and down and rotate it weirdly. How to make my image to rotate normally?, as a small image. Then I have to be able to get the color touched. I get an answer here, but I still have to work at it. But first, please tell me how could I solve the problem with rotation, and if you have ideas about the second issue, with getting the color touched I would like to hear them.
this is the image:
this is how it should appear on the screen:
XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView android:id="#+id/imag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/roata"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-200dp"
android:scaleType="center"/>
</RelativeLayout>
Java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
picture = (ImageView)findViewById(R.id.imag);
picture.setOnTouchListener(onTableTouched);
}
public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent evt)
{
double r = Math.atan2(evt.getX() - picture.getWidth() / 2, picture.getHeight() / 2 - evt.getY());
int rotation = (int) Math.toDegrees(r);
if (evt.getAction() == MotionEvent.ACTION_DOWN)
{
//
}
if (evt.getAction() == MotionEvent.ACTION_MOVE)
{
updateRotation(rotation);
}
if (evt.getAction() == MotionEvent.ACTION_UP)
{
//
}
return true;
}
};
private void updateRotation(double rot)
{
float newRot = new Float(rot);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);
Matrix matrix = new Matrix();
matrix.postRotate(newRot - 50);
Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
picture.setImageBitmap(redrawnBitmap);
}
UPDATE:
Because I got nowhere with Bitmap and Matrix I tried to adjust the RotateAnimtaion() to get something as similar as I could. I think modifying the second param and the duration of the animation dynamic, we can get a similar result without changing the Y position. Now, the problem is that the image is crop, I think because of scaleType="center". I will post code and a screen shoot. Could this be improved?
Animation a = new RotateAnimation(0.0f, param,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
a.setFillEnabled(true);
a.setFillAfter(true);
a.setDuration(param);
picture.startAnimation(a);

just declare Bitmap as globally as:
private WeakReference<Bitmap> bitmap;
and use
bitmap =new WeakReference(BitmapFactory.decodeResource(getResources(), R.drawable.roata));
instead of
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);

I think the problem is , you are trying to redraw the same Bitmap in every updateRotation() call.
How about removing this line,
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);
from updateRotation(double rot) and have it declared globally , so that you need not create the same Bitmap again and again which is why you are getting the OutOfMemory.
Try this,
Bitmap bitmap=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
picture = (ImageView)findViewById(R.id.imag);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);
picture.setOnTouchListener(onTableTouched);
}
public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent evt) {
double r = Math.atan2(evt.getX() - picture.getWidth() / 2, picture.getHeight() / 2 - evt.getY());
int rotation = (int) Math.toDegrees(r);
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
//
}
if (evt.getAction() == MotionEvent.ACTION_MOVE) {
updateRotation(rotation);
}
if (evt.getAction() == MotionEvent.ACTION_UP) {
//
}
return true;
}
};
private void updateRotation(double rot) {
float newRot = new Float(rot);
Matrix matrix = new Matrix();
matrix.postRotate(newRot - 50);
Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
picture.setImageBitmap(redrawnBitmap);
}

Related

Draw bitmap on imageview with every touch - android

In my imageview I want to draw a bitmap everytime imageview is touched without erasing the previous bitmap. Below code draws a new bitmap but also erases the previous one. How can I keep the previous bitmap while adding new one? Thanks
imageview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
x = event.getX();
y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Bitmap.Config config = bm.getConfig();
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap bm2 = Bitmap.createBitmap(width, height, config);
Canvas c = new Canvas(bm2);
c.drawBitmap(bm, 0, 0, null);
Bitmap repeat = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
Bitmap repeat2 = Bitmap.createScaledBitmap(repeat, 50, 50, false);
c.drawBitmap(repeat2, x, y, p);
imageview.setImageBitmap(bm2);
break;
return true;
}
});
}
If you will use the same bitmap each time...
Make the Bitmap a member variable and initialize it in your onCreate system.
Create an ArrayList as a member variable.
Add a new Point each time you touch the ImageView.
Loop through the Point List and draw the same Bitmap onto your ImageView canvas.
I think you need to use an array of Bitmaps, then use the onClick to iterate through the array.
You need to have two layers for that. When you change some pixels of a bitmap, it won't be able to handle two layers. So you need to create another layer on image View and set new bitmap over there.
you have to keep track of pixels you parsed through in an arraylist, so that you can you can handle as eraser also by resetting those pixels to transparent.
Do it like this.
Bitmap bm2 = null;
Canvas c = null;
imageview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
x = event.getX();
y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Bitmap.Config config = bm.getConfig();
int width = bm.getWidth();
int height = bm.getHeight();
if(bm2==null){
bm2 = Bitmap.createBitmap(width, height, config);
c = new Canvas(bm2);
}
c.drawBitmap(bm, 0, 0, null);
Bitmap repeat = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
Bitmap repeat2 = Bitmap.createScaledBitmap(repeat, 50, 50, false);
c.drawBitmap(repeat2, x, y, p);
imageview.setImageBitmap(bm2);
break;
return true;
}
});
}

Rotate around alternate axis

I am struggling with bitmap rotations. I wish to rotate a graphic around an alternate axis but I can only ever get it to rotate around the center point no matter what I do, putting in postTranlate. preTranslate, set Translate in any order doesnt work I have also tried the postRotate(45,0,0) but it always rotates around the center.
Code below taken of internet what would I do to alter its rotation point, the code below uses the launcher icon which is square I am using a long thin graphic like an arrow.
// Rotate image to 45 degrees.
public void RotateImage(){
ImageView image;
Bitmap bMap;
Matrix matrix;
//Get ImageView from layout xml file
image = (ImageView) findViewById(R.id.imageView1);
//Decode Image using Bitmap factory.
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//Create object of new Matrix.
matrix = new Matrix();
//set image rotation value to 45 degrees in matrix.
matrix.postRotate(45);
//Create bitmap with new values.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
bMap.getWidth(), bMap.getHeight(), matrix, true);
//put rotated image in ImageView.
image.setImageBitmap(bMapRotate);
}
I have tried the code below but its still rotates around the center or at least appears too
public void RotateImage{
ImageView image;
Bitmap bMap;
Matrix matrix;
//Get ImageView from layout xml file
image = (ImageView) findViewById(R.id.imageView);
//Decode Image using Bitmap factory.
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//Create object of new Matrix.
matrix = new Matrix();
//set image rotation value to 45 degrees in matrix.
matrix.setTranslate(-100,-200);
matrix.postRotate(angle);
matrix.postTranslate(100,200);
//Create bitmap with new values.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
bMap.getWidth(), bMap.getHeight(), matrix, true);
//put rotated image in ImageView.
image.setImageBitmap(bMapRotate);
Thanks
If you want your Bitmap to rotate 45 degree around x axis with pivot (a,b), you can call matrix.rotate(45, a, b)
Perhaps you want to use the Camera class to rotate around X, Y or Z axis:
matrix = new Matrix();
Camera camera = new Camera();
camera.save();
camera.rotateX(45f);
camera.getMatrix(matrix);
camera.restore();
The way I understand your question is that you want to rotate the image around some point, say (x, y). Conceptually you need to perform the following transformations on the image:
Translate by (-x, -y)
Rotate by 45 degrees
Translate by (x, y)
You can use this method to rotate an object from center create this method in sprite class
public void paintFromCenter(float angle, Canvas c) {
Bitmap b = sprite;
Bitmap h = b;
// Canvas canvas = new Canvas(a);
Matrix matrix = new Matrix();
matrix.postRotate(angle, h.getWidth() / 2, h.getHeight());
matrix.postTranslate(getX(), getY());
// canvas.drawBitmap(bitmap, matrix, new Paint());
Bitmap bmp2 = Bitmap.createBitmap(h, 0, 0, frameWidth, frameHeight,
matrix, true);
c.drawBitmap(h, matrix, null);
// g.getCanvas().drawBitmap(bmp2, getX(), getY(), null);
}
Now in onTouchEvent()
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
evX = (int) event.getX();
evY = (int) event.getY();
int initX = objectSprite.getX();
int inity = objectSprite.getY();
if ((evX > objectSprite.getX()
&& evX < objectSprite.getX() + objectSprite.getWidth()
&& evY > objectSprite.getY() && evY < objectSprite.getY()
+ objectSprite.getHeight())) {
if (angle < 90) {
angle += 5;
} else if (angle < -180)
angle -= 5;
}
}
return true;
}
in draw() method paint the image/object
private void draw(Canvas canvas) {
objectSprite.paintFromCenter(angle, canvas);
}
try it

Android - adding a custom view to scroll view where the custom view is an inner class

I've been trying to enable my application to scroll my custom view (both horizontally and vertically) to enable the user to draw on more than just the initial screen size. The application is to be used for basic house plan generation e.g. a user draws connected rectangles representing their home.
I am using a custom View which I have set up (as an inner class) inside the activity. After extensive searching and seeing the issues people are having with scroll views, this is as far as I've managed to get. Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.example.HomerProject1stDraft.DrawNewPlans.HomerView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.HomerProject1stDraft.DrawNewPlans.HomerView>
</LinearLayout>
As you can see I'm trying to add my HomerView class which is inside the DrawNewPlans class (the activity).
Here is the onCreate() method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawnew);
}
Here's the code for my custom view (bearing in mind this is contained in another class):
class HomerView extends View { // the custom View for drawing on
// set up Bitmap, canvas, path and paint
private Bitmap myBitmap; // the initial image we turn into our canvas
private Canvas myCanvas; // the canvas we are drawing on
private Rect myRect; // the mathematical path of the lines we draw
private Paint myBitmapPaint; // the paint we use to draw the bitmap
// get the width of the entire tablet screen
private int screenWidth = getContext().getResources()
.getDisplayMetrics().widthPixels;
// get the height of the entire tablet screen
private int screenHeight = (getContext().getResources()
.getDisplayMetrics().heightPixels);
private int mX, mY, iX, iY; // current x,y and in
public HomerView(Context context) { // constructor of HomerView
super(context);
System.out.println("Screen Width = "+screenWidth+" and screen height = "+screenHeight);
myBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
Bitmap.Config.ARGB_8888); // set our drawable space - the bitmap which becomes the canvas we draw on
myCanvas = new Canvas(myBitmap); // set our canvas to our bitmap which we just set up
myRect = new Rect(); // make a new rect
myBitmapPaint = new Paint(Paint.DITHER_FLAG); // set dither to ON in our saved drawing - gives better color interaction
}
protected void onDraw(Canvas canvas) { // method used when we want to draw something to our canvas
super.onDraw(canvas);
if (addObjectMode == true || addApplianceMode == true) {
canvas.drawColor(Color.TRANSPARENT); // sets canvas colour
canvas.drawBitmap(myBitmap, 0, 0, myBitmapPaint); // save the canvas to bitmap - the numbers are the x, y coords we are drawing from
for (int i = 0; i < rectList.size(); i++) {
canvas.drawRect(rectList.get(i), myPaint);
}
if (addApplianceMode == true) {
canvas.drawBitmap(bmp, iX-50, iY-50, myBitmapPaint);
}
canvas.drawRect(myRect, myPaint); // draw the rectangle that the user has drawn using the paint we set up
}
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { // if screen size changes, alter the bitmap size
super.onSizeChanged(w, h, oldw, oldh);
}
private void touch_Start(float x, float y) { // on finger touchdown
// check touch mode
iX = (int) (Math.round(x));
iY = (int) (Math.round(y));
mX = (int) (Math.round(x));
mY = (int) (Math.round(y));
if (addObjectMode == true) {
myRect.set(iX, iY, mX, mY);
} else if (addApplianceMode == true) {
// code to draw an appliance icon at mX, mY (with offset so icon is centered)
if (isLamp == true) {
Resources res = getResources();
bmp = BitmapFactory.decodeResource(res, R.drawable.lamp);
myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint);
} else if (isPC == true) {
Resources res = getResources();
bmp = BitmapFactory.decodeResource(res, R.drawable.pc);
myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint);
} else if (isKettle == true) {
Resources res = getResources();
bmp = BitmapFactory.decodeResource(res, R.drawable.kettle);
myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint);
} else if (isOven == true) {
Resources res = getResources();
bmp = BitmapFactory.decodeResource(res, R.drawable.oven);
myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint);
} else if (isTV == true) {
Resources res = getResources();
bmp = BitmapFactory.decodeResource(res, R.drawable.tv);
myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint);
}
}
}
private void touch_Move(float x, float y) { // on finger movement
float dX = Math.abs(x - mX); // get difference between x and my X
float dY = Math.abs(y - mY);
if (dX >= TOUCH_TOLERANCE || dY >= TOUCH_TOLERANCE) { // if coordinates are outside screen? if touching hard enough?
mX = (int) (Math.round(x));
mY = (int) (Math.round(y));
if (addObjectMode == true) {
myRect.set(iX, iY, mX, mY);
}
}
}
#SuppressWarnings("deprecation")
private void touch_Up() { // on finger release
if (addObjectMode == true) {
myRect.set(iX, iY, mX, mY);
System.out.println("MyRect coords = "+iX+","+iY+","+mX+","+mY);
rectList.add(myRect);
myCanvas.drawRect(iX, iY, mX, mY, myPaint); // if this isnt present - no lasting copy is drawn to screen. It is erased every draw.
if (eraseMode == false) {
dialogStarter();
}
} else if (addApplianceMode == true) {
showDialog(DIALOG_DEVICE_ENTRY);
}
}
public boolean onTouchEvent(MotionEvent event) { // on any touch event
if (addObjectMode == true || addApplianceMode == true) {
float x = event.getX(); // get current X
float y = event.getY(); // get current Y
switch (event.getAction()) { // what action is the user performing?
case MotionEvent.ACTION_DOWN: // if user is touching down
touch_Start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE: // if user is moving finger while touched down
touch_Move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP: // if user has released finger
touch_Up();
invalidate();
break;
}
return true;
} else {
return false;
}
}
}
I realise there are many things I need to add - but what are they? awakenScrollBars()?
Also: at the moment I get this error Error inflating class com.example.HomerProject1stDraft.DrawNewPlans.HomerView in the XML file. Is there a mistake in the way I'm creating the custom view? Can you even create a view from an inner class?
Many Thanks.
Firstly , i would suggest not to wrap your custom scrollview in another scrollview.If it is avoidable, avoid it.Also, you can check below link where i have implemented a custom scrollview.
https://stackoverflow.com/questions/15188842/customization-of-imageview-and-layouts
ScrollView only supports verticall scrolling. You need scrolling on both sides horizontal and vertical for that you will need a special class known as TwoDScrollView.
more information about this class and source code is available at:
http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/
In your xml use this class instead of ScrollView and you should be able to scroll both horizontally and vertically.
For your other error please share the source code and state where you get the problem.

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

I am having Image Quality problems using Canvas and canvas.scale(Scale, Scale); they look exactly like the following:
android: quality of the images resized in runtime
I believe I have read all the posts on image quality problems when re-sizing bitmaps, but it doesn't seem to help when scaling with a Canvas scale(float scale).
I have tried many different options as suggested by the image quality posts.
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);
I believe this the last barrier to achieving my goals. I am quite concerned as I am in trouble if I can't get the image quality problem solved. Any help is appreciated.
Thanks!
The system will not let me post a picture, so following is a link.
PictureSample
I don't no whether my answer is correct or not i have a code for canvas. i wish this might help you.
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);
}
}
}
Do you have a screenshot that would show the quality issue you are talking about? If filtering is enabled on the Paint it should be fine.
I tried many and many code.
I just add this in my manifest and I had the best quality image.
<uses-sdk android:minSdkVersion="9" />
I think the problem is the canvas is created in low resolution. If u try to print on the screen the resolution of your device, u can see it is wrong.
With the add in the manifest the resolution change.

Flipping a bitmap in android help?

I want to save up memory for my game and I wanted to ask you because I couldn't find anything and last time that I asked something here I got a good answer. Can i flip the bitmap inside eclipse so i could save on memory for sprites? All the tutorials that i found were about rotating and not flipping. The tutorials for flipping a bitmap were only for open Gl or something like that. Please help me.
I've been looking up for tutorials in google but i gave up at page 5. Can anyone help me?
Does anyone have a good tutorial?
By the way I am using a canvas.
Thanks!
I get a force close everytime I try to run it... can u figure it out? here is my code:
Matrix flipHorizontalMatrix = new Matrix();
flipHorizontalMatrix.setScale(-1,1);
flipHorizontalMatrix.postTranslate(0, canvas.getHeight()-arrowL.getHeight());
canvas.drawBitmap(arrowL, flipHorizontalMatrix, null);
I want the arrow to be at the bottom right corner.
Since you're using Canvas, why not try the drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) method. Use a Matrix that flips the x coordinates.
You can do something like this:
Matrix flipHorizontalMatrix = new Matrix();
flipHorizontalMatrix.setScale(-1,1);
flipHorizontalMatrix.postTranslate(myBitmap.getWidth(),0);
canvas.drawBitmap(myBitmap, flipHorizontalMatrix, myPaint);
Thanks,check out this code it may be useful for you to rotate an Bitmap image..
Here i have a taken a aquarium fish example,it should be moved from left to right and flipped and continue moving from right to left and viceversa..
here is the code for you..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
fish = BitmapFactory.decodeResource(getResources(), R.drawable.fish);
v = new OurView(this);
public class OurView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isitOK = false;
String Flag = "right";
Bitmap rotatedBitmap=null;
Matrix rotateRight = new Matrix();
Matrix rotateLeft = new Matrix();
Bitmap rSprite=null;
Bitmap lSprite=null;
public OurView(Context context) {
super(context);
holder = getHolder();
rotateLeft.setScale(-1, 1);
rSprite = Bitmap.createBitmap(fish, 0, 0,
fish.getWidth(), fish.getHeight(), rotateRight, true);
lSprite = Bitmap.createBitmap(fish, 0, 0,
fish.getWidth(), fish.getHeight(), rotateLeft, true);
}
#Override
public void run() {
// TODO Auto-generated method stub
while (isitOK == true) {
if (!holder.getSurface().isValid()) {
continue;
}
Canvas canvas = holder.lockCanvas();
canvas.drawBitmap(bg, 0, 0, null);
if(Flag == "right")
canvas.drawBitmap(lSprite, x, y, null);
if(Flag == "left")
canvas.drawBitmap(fish, x, y, null);
if (Flag == "right" && x <= 60) {
x++;
if (x == 60) {
Flag = "left";
// canvas.drawBitmap(rSprite, 0, fish.getWidth(), null);
canvas.drawBitmap(fish, x, y, null);
}
}
if (Flag == "left" && x >= 0) {
x--;
if (x == 0) {
Flag = "right";
canvas.drawBitmap(fish, x, y, null);
}
}
holder.unlockCanvasAndPost(canvas);
}
}

Categories

Resources