Two methods of Android to realize image rotation effect

  • 2020-11-20 06:15:34
  • OfStack

In the process of using APP, you will often see the effect of the above banner image polling. So today, let's get up and learn 1.

Method 1: Animation method: (cumbersome code)

This distribution needs: two animation effects, 1 layout, 1 main class to achieve, not to say, look at the code:


public class IamgeTrActivity extends Activity {
/** Called when the activity is first created. */
public ImageView imageView;
public ImageView imageView2;
public Animation animation1;
public Animation animation2;
public TextView text;
public boolean juage = true;
public int images[] = new int[] { R.drawable.icon, R.drawable.expriment,
R.drawable.changer, R.drawable.dataline, R.drawable.preffitication };
public int count = 0;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView  To go out  imageView2  Come in 
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setBackgroundResource(images[count % 5]);
count++;
imageView2.setBackgroundResource(images[count % 5]);
text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i(handler, handler);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
// will iamgeView First hide, then show 
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}
public void onPause() {
juage = false;
super.onPause();
}
}

Layout code:


android:orientation=vertical
android:layout_width=fill_parent
android:layout_height=fill_parent
android:id=@+id/rl>
android:id=@+id/imageView
android:layout_width=fill_parent
android:background=@drawable/icon
android:layout_below=@+id/rl
android:layout_height=120dp />
android:id=@+id/imageView2
android:layout_width=fill_parent
android:background=@drawable/expriment
android:layout_below=@+id/rl
android:layout_height=120dp />
android:id=@+id/text
android:layout_width=fill_parent
android:layout_height=wrap_content
android:layout_below=@id/imageView/>

Second, ViewFlipper is used to realize image rotation

Android system comes with a multi-page management control, which can realize the automatic switch of sub-interface:

First you need to add View to ViewFlipper

(1) Static import: import directly in the layout layout file

(2) Dynamic import: addView() method

Common methods of ViewPlipper:

setInAnimation: Sets the animation to use when View enters the screen

setOutAnimation: Sets the animation to use when View exits the screen

showNext: Call this function to display the next View in ViewFlipper

showPrevious: Call this function to display the last View in ViewFlipper

setFlipInterval: Sets the time interval between View switches

startFlipping USES the interval set above to start switching all View and the switch loops

stopFlipping: Stop the View switch

So with that said, what are we going to achieve today?

(1) Use ViewFlipper to realize image rotation

(2) ViewFlipper which supports gesture sliding

We need to prepare a few images first: put the images into drawable

Create two animations: under res create one new folder create two new xml:


left_in:
android:duration=5000
android:fromXDelta=100%p
android:toXDelta=0/>
left_out:
android:fromXDelta=0
android:toXDelta=-100%p
android:duration=5000/>

1 layout file:


xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_parent
android:layout_height=match_parent
tools:context=.MainActivity >
android:id=@+id/flipper
android:layout_width=fill_parent
android:layout_height=fill_parent/>

1 main class:


public class MainActivity extends Activity {
private ViewFlipper flipper;
private int[] resId = {R.drawable.pc1,R.drawable.pc2,R.drawable.pc3,R.drawable.pc4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flipper = (ViewFlipper) findViewById(R.id.flipper);
/*
*  Dynamic import is as follows ViewFlipper Join the son View
* */
for (int i = 0; i < resId.length; i++) {
flipper.addView(getImageView(resId[i]));
}
/*
*  for ViewFlipper To add animation 
* */
flipper.setInAnimation(this, R.anim.left_in);
flipper.setOutAnimation(this, R.anim.left_out);
flipper.setFlipInterval(5000);
flipper.startFlipping();
}
private ImageView getImageView(int resId){
ImageView image = new ImageView(this);
image.setBackgroundResource(resId);
return image;
}
}

So this has achieved the function effect of 1 picture polling

We can also add click and swipe effects:

We also need to add two more sliding effects to the right:


right_in:
android:fromXDelta=0
android:toXDelta=-100%p
android:duration=2000/>
right_out:
android:fromXDelta=100%p
android:toXDelta=0
android:duration=2000/>

Then we need to add to the main class (if you don't want the image to auto-play and just want to do it with gestures then you need to delete the "add Animation code for ViewFlipper") :


publibooleaonTouchEvent(MotionEvenevent{
/TODAuto-generatemethostub
switc(event.getAction(){
casMotionEvent.ACTION_DOWN:
startevent.getX();
break;
casMotionEvent.ACTION_MOVE:// Determine whether to slide left or right 
i(event.getX(start100{
flipper.setInAnimation(thisR.anim.left_in);
flipper.setOutAnimation(thisR.anim.left_out);
flipper.showPrevious();
}elsi(startevent.getX(100{
flipper.setInAnimation(thisR.anim.right_in);
flipper.setOutAnimation(thisR.anim.right_out);
flipper.showNext();
}
casMotionEvent.ACTION_UP:
break;
}
retursuper.onTouchEvent(event);
}

This completes our image polling capability with our ViewFlipper.

Above is the site to share Android to achieve image rotation effect of the two ways, I hope you like.


Related articles: