Android 尝试旋转图像时程序停止
我是android编程新手,正在尝试旋转图像 这是我的代码:Android 尝试旋转图像时程序停止,android,image,animation,imageview,rotateanimation,Android,Image,Animation,Imageview,Rotateanimation,我是android编程新手,正在尝试旋转图像 这是我的代码: public class MainActivity extends Activity { ImageView x; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_m
public class MainActivity extends Activity {
ImageView x;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = (ImageView)findViewById(R.drawable.clippedneedle);
RotateAnimation newAnim = new RotateAnimation(0,360);
newAnim.setFillAfter(true);
newAnim.setDuration(10);
x.startAnimation(newAnim);
}
}
我的布局文件是标准的,只包含ImageView中的代码
基本上,我的程序一打开就停止运行。logcat表示线程正在退出,但存在未捕获的异常
任何帮助都将不胜感激。谢谢 您正在此处传递一个
可绘制的的id:
x = (ImageView)findViewById(R.drawable.clippedneedle);
您需要传递图像视图的id
。如果在R.layout.activity\u main
中有ImageView
,请执行以下操作:
x = (ImageView)findViewById(R.id.myImageViewId);
x.setImageDrawable(getResources().getDrawable(R.drawable.clippedneedle
以下是您的代码,其中有一些更改:
x = (ImageView)findViewById(R.drawable.clippedneedle);
RotateAnimation newAnim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
newAnim.setFillAfter(true);
newAnim.setDuration(2000); // 2 seconds || 2000 milliseconds
x.startAnimation(newAnim);
你可以在这里做更多的事情。查看。您正在传递一个可绘制的的id,如下所示:
x = (ImageView)findViewById(R.drawable.clippedneedle);
您需要传递图像视图的id
。如果在R.layout.activity\u main
中有ImageView
,请执行以下操作:
x = (ImageView)findViewById(R.id.myImageViewId);
x.setImageDrawable(getResources().getDrawable(R.drawable.clippedneedle
以下是您的代码,其中有一些更改:
x = (ImageView)findViewById(R.drawable.clippedneedle);
RotateAnimation newAnim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
newAnim.setFillAfter(true);
newAnim.setDuration(2000); // 2 seconds || 2000 milliseconds
x.startAnimation(newAnim);
你可以在这里做更多的事情。看看。你能发布完整的logcat错误stacktrace吗?我编辑了我的答案,并在底部添加了一些代码。我已经测试过了,它可以工作。你能发布你的完整logcat错误stacktrace吗?我编辑了我的答案,并在底部添加了一些代码。我已经测试过了,它可以正常工作。或者只是x.setImageResource(R.drawable.clippedneedle);不确定我在哪里读到过关于setImageDrawable(Drawable)比setImageResource(int)更高效的内容。在这种情况下这并不重要。谢谢,程序现在运行,但它不旋转。。您知道为什么会发生这种情况吗?或者只知道x.setImageResource(R.drawable.clippedneedle);不确定我在哪里读到过关于setImageDrawable(Drawable)比setImageResource(int)更高效的内容。在这种情况下这并不重要。谢谢,程序现在运行,但它不旋转。。你知道为什么会发生这种情况吗?