I'm trying to rotate the image 360 ​​by clicking it, but it can't rotate it?

I'm trying to rotate the image 360 ​​by clicking it myself, but it doesn't work

I am using an xml file like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="72dp" android:layout_height="72dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="83dp" android:layout_marginTop="103dp" android:clickable="true" android:src="@drawable/cute" /> </RelativeLayout> 

and java code like this ...

 package com.example.testapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.RotateAnimation; import android.widget.ImageButton; public class MainActivity extends Activity implements OnClickListener { ImageButton img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageButton) findViewById(R.id.imageView1); } public void onClick(View v) { // TODO Auto-generated method stub RotateAnimation ra =new RotateAnimation(0, 360); ra.setFillAfter(true); ra.setDuration(0); img.startAnimation(ra); } } 

My purpose of the application is to use 9 img btn development and puzzle games.

+4
source share
2 answers

Your code is working fine. the problem is that you set the duration to 0. You don’t see this happening because if you rotate 360 ​​degrees, its end position and start position will be the same. therefore, set the duration to 1000 (second) or more to see how the animation happens.

just try:

 ra.setDuration(1000); 

And you also did not install the img onClick listener.

just try:

 img .setOnClickListener(new OnClickListener() { public void onClick(View arg0) { //animation here } }); 
+7
source

This was the code I used to rotate in one small application.

 public class MainScreen extends Activity{ ImageView ivDH = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.screen); ivDH = (ImageView) findViewById(R.id.dollhouse); ivDH.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub startAnimation(ivDH); return false; } }); } public void startAnimation(ImageView ivDH) { System.out.println("Inside startAnimation()"); Animation scaleAnim = new ScaleAnimation(0, 2, 0, 2); scaleAnim.setDuration(5000); scaleAnim.setRepeatCount(1); scaleAnim.setInterpolator(new AccelerateInterpolator()); scaleAnim.setRepeatMode(Animation.REVERSE); Animation rotateAnim = new RotateAnimation(0, 360, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF); rotateAnim.setDuration(5000); rotateAnim.setRepeatCount(1); rotateAnim.setInterpolator(new AccelerateInterpolator()); rotateAnim.setRepeatMode(Animation.REVERSE); AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(scaleAnim); animationSet.addAnimation(rotateAnim); ivDH.startAnimation(animationSet); } } 

You will have to change some parameters. Hope this helps.

+4
source

Source: https://habr.com/ru/post/1444304/


All Articles