我在制作一个ImageView(dbm_needle)时出现问题,我想在按下按钮后使用下面的代码旋转,在RelativeLayout上正确显示。内容似乎正确绘制开始,但按钮按下似乎没有启动使针旋转的onTouch事件,我似乎无法弄清楚出了什么问题。
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);
}
}
答案 0 :(得分:1)
堆栈跟踪似乎显示内部错误。有时日食会毫无理由地搞砸。尝试清理项目或重新启动eclipse或重新启动adb。这可能会解决您的问题。
然后我认为您应该将布局参数添加到像
这样的imageview中imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT
,LayoutParams.WRAP_CONTENT));