2011-11-21 68 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)); 
相关问题