Reputation: 116
I am new to android programming and am trying to rotate an image.
This is my code:
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);
}
}
My layout file is standard and just contains code from an ImageView.
Basically, my program stops running as soon as it opens. The logcat says that the thread is exiting with uncaught exception.
Any help would be much appreciated. Thanks!
Upvotes: 1
Views: 71
Reputation: 51571
You are passing the id of a Drawable
here:
x = (ImageView)findViewById(R.drawable.clippedneedle);
You need to pass the id of an ImageView
. If you have an ImageView
in R.layout.activity_main
, do the following:
x = (ImageView)findViewById(R.id.myImageViewId);
x.setImageDrawable(getResources().getDrawable(R.drawable.clippedneedle
Here's your code with a few changes:
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);
There is much more that you can do here. Take a look at Android developer's resource page on Animation.
Upvotes: 1