2011-11-21 71 views
0

我有一種製造ImageView的(dbm_needle)的問題,我想用下面的代碼的按鈕推後正確地對RelativeLayout的旋轉,顯示不顯示。內容似乎是正確繪製的開始,但按鈕推動似乎並沒有啓動onTouch事件,使針旋轉,我似乎無法弄清楚什麼是錯的。的ImageView在RelativeLayout的

counter.xml:

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/dblmeter_screen" 
android:gravity="center_horizontal"> 

Java的活動:

public class Counter extends Activity { 

private boolean keepRotating = false; 
private boolean returnRotating = true; 
private Random mRnd = new Random(); 
private static final int INTERVAL = 25; 
int degrees =-65; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
          WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.counter); 
} 

public void make(float x){ 
    RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.dblmeter_screen) ; 

    Bitmap rotate_needle = BitmapFactory.decodeResource(getResources(), R.drawable.dbm_needle); 

    int width = rotate_needle.getWidth(); 
    int height = rotate_needle.getHeight(); 
    int newWidth = 6; 
    int newHeight = 400; 

    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 
    matrix.postRotate(x); 

    Bitmap resizedBitmap = Bitmap.createBitmap(rotate_needle, 0, 0, 
         width, height, matrix, true); 

    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

    ImageView imageView = new ImageView(this); 

    imageView.setImageDrawable(bmd); 

    imageView.setScaleType(ScaleType.CENTER); 

    setContentView(relLayout); 
} 

public void make(int degrees) 
{ 
    Log.i(this.getClass().toString(), "Rotation : " + degrees); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    switch (event.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
      startRotating(); 
      break; 
     case MotionEvent.ACTION_UP: 
      stopRotating(); 
      break; 
    } 

    return super.onTouchEvent(event); 
} 

public void startRotating() 
{ 
    returnRotating = false; 
    if (!keepRotating) 
    { 
     keepRotating = true; 

     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       if (keepRotating) 
       { 
        int Rand = mRnd.nextInt(30); 
        degrees = (degrees + (Rand/2)-Rand/6) % 360; 
        if(degrees > 65) 
         degrees = degrees - Rand; 
        make(degrees); 

        handler.postDelayed(this, INTERVAL); 
       } 
      } 
     }, INTERVAL); 
    } 
} 

public void stopRotating() 
{ 
    keepRotating = false; 
    if (!returnRotating) 
    { 
     returnRotating = true; 

     final Handler handler = new Handler(); 

     handler.postDelayed(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       if (returnRotating) 
       { 
        int Rand = mRnd.nextInt(30); 
        degrees = (degrees - (Rand/2)+Rand/6) % 360; 
        if(degrees < -65) 
         degrees = degrees + Rand; 
        make(degrees); 

        handler.postDelayed(this, INTERVAL); 
       } 
      } 
     }, INTERVAL); 
    } 
} 

public void pushClick(View pushClick) { 
    switch (pushClick.getId()) { 
    case R.id.btn_push: 
     make(degrees); 
     } 

    } 
+3

創建一如既往的ImageView的,任何你問一個強制關閉的時間,從發佈的logcat中堆棧跟蹤。它使你更容易幫助你。另外,「度」在哪裏申報?它有沒有設置? – kabuko

+0

這是一個瘋狂的課程。 –

+0

我解決了我的問題,通過刪除setContentView(relLayout);部分代碼發佈 – n00bdev

回答

1

堆棧跟蹤似乎表明一個內部錯誤。有時月蝕可能無緣無故地搞砸了。嘗試清理項目或重新啓動eclipse或重新啓動adb。這可能會解決您的問題。

那麼我認爲你應該添加布局參數像

imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT 
     ,LayoutParams.WRAP_CONTENT)); 
相關問題