2012-04-16 6 views
0

here (터치/플링에서 이미지를 동적으로 회전시키는) 튜토리얼을 가져 와서 하나의 이미지 대신 전체 레이아웃으로 리팩터링하려고했습니다. 그래서 나는 지금처럼 TutorialActivity에서 rotateDialer() 메소드를 변경했습니다 :레이아웃을 안드로이드로 회전하려고 시도합니다. 캔버스가 회전하지 않습니다.

걸기 내가 그러나 많은 도로가에서 설정 한 캔버스를 회전 재정의 된 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); 
} 

setNextRotation() 메서드를 사용하지만 캔버스가 회전하는 것처럼 보이지 않습니다!

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

왜 캔버스가 눈에 띄게 회전하지 않습니다

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는 : 여기 내 사용자 지정 레이아웃은? 각도 계산 후에 onTouch() 다이얼러가 무효화되어 계산이 올바르게 작동하지만 (보기에 로그에서 값이 변경됨) 뷰가 회전하지 않는 것 같습니다. 잘못된?

+0

나는 이것이 오래된 게시물이라는 것을 알고 있지만 이것을 이해하는 데 단지 2 시간을 소비한다. 위는 canvas.save()를 움직이면 작동 할 것이다. before canvas.rotate (degrees, px, py); – Warpzit

답변

2

확인이 메서드가 호출되고 있지 않았다 캔버스는 onDraw()를 밝혀, 나는 단지 내 초기화() 함수에

setWillNotDraw(false) 

을 추가했고, 프레스토 헤이 된 onDraw() 메소드가 호출되고 있으며, 내 견해가 돌고있다 !!!!

+0

작동하는 코드를 공유 할 수 있습니까?, 위에서 말했듯이 Thumbx – Udaykiran

+0

, 그냥 init() 함수에 "setWillNotDraw (false)"라고 써야했습니다. 그 라인을 클래스의 생성자 메소드에두면 작업 할 수 있습니다. 원래 질문 자체의 원래 코드 스 니펫은이 작업을 수행하여 자유롭게 복사하여 DialView() 생성자 또는 init() 함수에 행을 삽입합니다. – AndroidNoob

+0

코드를 삽입했지만 뷰가 남아 있습니다. 같은, .. 전혀 회전하지 않습니다 .. 각도의 로그도 볼 수 있습니다 – Udaykiran

관련 문제