1

我是 android 编程新手,正在尝试旋转图像。

这是我的代码:

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 说线程正在以未捕获的异常退出。

任何帮助将非常感激。谢谢!

4

1 回答 1

1

您在这里传递 a 的 id Drawable

x = (ImageView)findViewById(R.drawable.clippedneedle); 

您需要传递一个ImageView. 如果您有ImageViewin R.layout.activity_main,请执行以下操作:

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

你可以在这里做更多的事情。查看Android 开发人员关于 Animation 的资源页面

于 2013-07-18T01:00:25.720 回答