触摸Android旋转具有按钮或图像的布局 [英] Rotate layout having buttons or images with touch Android

查看:105
本文介绍了触摸Android旋转具有按钮或图像的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




可以提出一些示例代码,通过这些示例代码,我们可以旋转具有触摸图像的布局..从2周开始尝试,但无法成功.

请检查我的代码,我无法明显旋转画布


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


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; 
    }
    
   
}



在OnDraw方法中也获得了角度度值,但是视图没有旋转.


谢谢
Uday



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


Thanks
Uday

推荐答案

如果您使用的是xml文件进行布局,请在"res/layout-land/"中创建另一个同名布局文件,并在此处创建景观布局.
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.


> http://stackoverflow.com/questions/8712652/rotating-image-on- a-canvas-in-android [ ^ ]
http://stackoverflow.com/questions/10174897/trying-to-rotate-a-layout-in-android-canvas-does-not-appear-to-rotate[^]
http://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in-android[^]


这篇关于触摸Android旋转具有按钮或图像的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆