Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,


Can any suggest some sample codes, with which we can rotate layout having images on touch .. am trying from 2 weeks but am not able to succeed.

Please check my code , i cant visibly rotate the canvas


Java
private void rotateDialer(float degrees) {
		//matrix.postRotate(degrees, dialerWidth / 2, dialerHeight / 2);
		
		
		//dialer.setImageMatrix(matrix);
		
		 layout.setNextRotation(degrees, dialerWidth / 2, dialerHeight  / 2,matrix);
		// view.requestLayout();
	//	 view.requestFocus();
		 layout.postInvalidate();
		 layout.invalidate();
		// view.invalidate();
		
		
		 //   dialer.invalidate();
	}


and this my custom view class :

<pre lang="java">package de.vrallev.tutorial.dialer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class RotateLinearLayout extends RelativeLayout{

    private Matrix mForward = new Matrix();
    private Matrix mReverse = new Matrix();
    private float[] mTemp = new float[2];
    
    AbsoluteLayout dialView;
    
    ImageView ab,fc,pic,co,pr,ref,tw,you;

    private Context mContext;
    private ArrayList<ImageView> imageViews=new ArrayList<ImageView>();
    private Integer[] drawables={R.drawable.aboutus_btn,R.drawable.coworkers_btn,R.drawable.profile_btn,R.drawable.picasa_btn,R.drawable.youtube_btn,R.drawable.twitter_btn,R.drawable.facebook_btn,R.drawable.referral_btn};
   
    float degrees;
    float px,screenW;
    float py,screenH;
    
    
    public RotateLinearLayout(Context context) {
        super(context);
        mContext=context;
        init();
    }

    public RotateLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init();
    }
    
    public void init(){
    	
    	setWillNotDraw(false);
    	 LayoutInflater inflator = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	 dialView = (AbsoluteLayout) inflator.inflate(R.layout.new_view, this,false);
    	
    
    	  
    	    addView(dialView);
    	   
    	}

    
    	public void setNextRotation(float degrees, float px, float py,Matrix matrix)
    	{
    	    this.degrees = degrees;
    	    this.px = px;
    	    this.py = py;
    	    screenW=px;
    	    screenH=py;
    	    System.out.println("....next");
    	   // invalidate();
    	}
    	
    @Override
    protected void onDraw(Canvas canvas) {

    	//Bitmap.Config config = loadBitmapFromView(this).getConfig();
    	//Bitmap targetBitmap = Bitmap.createBitmap((loadBitmapFromView(this)).getWidth(), (loadBitmapFromView(this)).getHeight(),config);
    	canvas.save(Canvas.MATRIX_SAVE_FLAG);
		{
			canvas.translate(px,py);

			float rotation = degrees;
			float midX = px / 2.0f;
			float midY = py / 2.0f;

			canvas.rotate(rotation, midX, midY);
			//outerLayer.drawOn(canvas, 0, 0);
			canvas.rotate(- rotation, midX, midY);
		}		
		canvas.restore();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        event.setLocation(getWidth() - event.getX(), getHeight() - event.getY());
        return super.dispatchTouchEvent(event);
    }
    
    @Override     
    public void onSizeChanged (int w, int h, int oldw, int oldh){ 
      super.onSizeChanged(w, h, oldw, oldh);         

      screenW = w;         
      screenH = h; 
    }
    
   
}


am getting angle degree values in OnDraw method also, but view is not rotating.


Thanks
Uday
Posted
Updated 29-Aug-12 10:08am
v2
Comments
[no name] 29-Aug-12 15:05pm    
This, http://www.bing.com/search?q=android+rotate+image&FORM=SAWRES&src=IE-SearchBox&adlt=strict, search gave me over 18 million results. Might be that some of them might help you.
UdayKiran Gudepu 29-Aug-12 15:08pm    
Am looking for rotating layout, not an image .. i tried rotate layout but its not moving around its center so am looking for working sample
[no name] 29-Aug-12 15:10pm    
Sorry. This, http://www.bing.com/search?q=android+rotate+layout&FORM=SAWRES&src=IE-SearchBox&adlt=strict, search return 1.8 million results. Maybe that will help you.

If you're using an xml file for you layout, create a second layout file in "res/layout-land/" with the same name, and create your landscape layout there.
 
Share this answer
 
Comments
UdayKiran Gudepu 29-Aug-12 15:15pm    
Sorry , but am looking for rotating layout using canvas
UdayKiran Gudepu 29-Aug-12 15:36pm    
Can you help me on this please
lewax00 29-Aug-12 15:41pm    
Sorry, I haven't used canvases before. But if you add some more details to your question (i.e. specifically what's not right, the code you're using, etc.) someone else might be able to.
 
Share this answer
 
Comments
UdayKiran Gudepu 29-Aug-12 16:09pm    
Please check my code am using, i can't visibly rotate the view but am getting angles

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900