2012-04-16 21 views
0

私はチュートリアルhere(タッチ/フリンジでイメージを動的に回転させる)を取って、1つのイメージではなく全体のレイアウトでリファクタリングしようとしました。ダイヤラは私がに設定されたキャンバスしかし、多くの度回転するオーバーライドされたonDraw()メソッドを使用して作成したカスタムレイアウトであるアンドロイドでレイアウトを回転しようとすると、キャンバスが回転しないように見える

private void rotateDialer(float degrees) { 
    //matrix.postRotate(degrees, dialerWidth/2, dialerHeight/2); 
    //dialer.setImageMatrix(matrix); 
    dialer.setNextRotation(degrees, dialerWidth/2, dialerHeight/2); 
    dialer.invalidate(); 
    info.setText("Current Quadrant Touched = "+currentQuadrantTouched 
      +"\nAngle = "+degrees); 
} 

:私はそうのようなTutorialActivityからrotateDialer()メソッドを変更しましたsetNextRotation()メソッドはキャンバスが回転していないようです!ここに私のカスタムレイアウトは次のとおりです。

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.graphics.drawable.BitmapDrawable; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 

public class DialView extends FrameLayout { 
public static final String LOG_TAG = "DialView"; 
RelativeLayout dialView; 
float degrees; 
float px; 
float py; 

public DialView(Context context) { 
    super(context); 
    init(); 
    // TODO Auto-generated constructor stub 
} 

public DialView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
    // TODO Auto-generated constructor stub 
} 
public DialView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
    init(); 
} 


private void init() 
{ 
    //initialise rotation 
    this.degrees = 0; 
    this.px = 0; 
    this.py = 0; 
    LayoutInflater inflator = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    dialView = (RelativeLayout) inflator.inflate(R.layout.dial_view, this,false); 
    addView(dialView); 
    Log.d(LOG_TAG, "DialView: init() "+ degrees+"degrees around ("+getWidth()/2+","+getHeight()/2+")"); 

} 



public void setNextRotation(float degrees, float px, float py) 
{ 
    this.degrees = degrees; 
    this.px = px; 
    this.py = py; 
    Log.d(LOG_TAG, "Next Rotation = "+ degrees+"degrees around ("+px+","+py+")"); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.save(); 
    // canvas.rotate(45,<appropriate x pivot value>,<appropriate y pivot value>); 
    canvas.rotate(degrees, px, py); 
    Log.d(LOG_TAG, "onDraw: Rotation = "+ degrees+"degrees around ("+px+","+py+")"); 
    super.onDraw(canvas); 
    canvas.restore(); 

} 


} 

これはdial_view.xmlです:

<ImageView 
    android:id="@+id/topLeftImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="38dp" 
    android:clickable="true" 
    android:layout_marginTop="28dp" 
    android:src="@drawable/icon" /> 

<ImageView 
    android:id="@+id/topRightImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:clickable="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="50dp" 
    android:src="@drawable/icon" /> 

<ImageView 
    android:id="@+id/bottomLeftImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:clickable="true" 
    android:layout_marginBottom="24dp" 
    android:layout_marginLeft="22dp" 
    android:src="@drawable/icon" /> 

<ImageView 
    android:id="@+id/bottomRightImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:clickable="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="28dp" 
    android:src="@drawable/icon" /> 

</RelativeLayout> 

なぜキャンバスは目に見えて回転しませんか?私は度の計算後にonTouch()のダイヤラーを無効にしています。計算が正しく機能しています(ログの値が変化しています)が、ビューは回転していないようです。違う?

+0

これは古い投稿ですが、これを理解するのに数時間を費やしています。上の例はcanvas.save()を移動すると機能します。以前はcanvas.rotate(degrees、px、py); – Warpzit

答えて

2

それはonDraw()メソッドが呼び出されていないされなかったが判明し[OK]を、私は単に私のinit()関数に

setWillNotDraw(false) 

を追加する必要がありましたし、ちょっとはonDraw()メソッドが呼び出されているさきがけて、私の意見は回転している!!!!

+0

私は上で述べたようにThanx – Udaykiran

+0

が動作しているコードを共有できますか?私はinit()関数に "setWillNotDraw(false)"と書かなければなりませんでした。クラスのコンストラクタメソッドの上にその行を置くと、そのクラスも動作します。元の質問の元のコードスニペットは、これを行うことで動作し、コピーしてDialView()コンストラクタまたはinit()関数のいずれかに自由に挿入してください – AndroidNoob

+0

私はコードを挿入しましたが、同じ、..まったく回転しません。角度のログも見ることができます – Udaykiran

関連する問題