Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of picture rotation in Android development

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the function of picture rotation in Android development". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve picture rotation function in Android development"!

Step 1: create the MainActivity class

Public class BaseActivity extends ActionBarActivity {private List mDatas; private ViewPager mViewPager; private boolean isRun = false; private AutoRunTask mTask; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); mViewPager = new ViewPager (this); mViewPager.setLayoutParams (new AbsListView.LayoutParams (LayoutParams.MATCH_PARENT, Util .getDimen (R.dimen.home_pic_height)); mViewPager.setAdapter (new HomePagerAdapter ()) / / set the initial position of the slide mViewPager.setCurrentItem (1000 * mDatas.size ()); mViewPager.setOnTouchListener (this); mTask = new AutoRunTask (); mTask.start ();} @ Override public boolean onTouch (View v, MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: mTask.stop (); break Case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mTask.start (); break; default: break;} return false; / / viewpager must be false, if it is true, it cannot be moved}}

Step 2: create an infinite loop PagerAdaper

Class HomePagerAdapter extends PagerAdapter {/ / use this to reuse imageView private LinkedList convertViews = new LinkedList (); @ Override public int getCount () {/ / return mPicUrls.size (); return Integer.MAX_VALUE; / / can wirelessly slide} @ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = = arg1 } @ Override public void destroyItem (ViewGroup container, int position, Object object) {if (object! = null & & object instanceof ImageView) {ImageView imageView = (ImageView) object; / / add to the collection convertViews.add (imageView); container.removeView (imageView);} super.destroyItem (container, position, object) } @ Override public Object instantiateItem (ViewGroup container, int position) {/ / position is large, so int index = position% mDatas.size (); ImageView imageView = null; if (convertViews! = null & & convertViews.size () > 0) {/ / take out an imageView imageView = convertViews.remove (0);} else {imageView = new ImageView (Util.getContext ()) } String uri = HttpHelper.URL + "image?name=" + mDatas.get (index); mBitmapUtils.display (imageView, uri); container.addView (imageView); return imageView;}}

Step 3: create a webcast class using handler

Public class AutoRunTask extends Handler implements Runnable {@ Override public void run () {while (isRun) {removeCallbacks (this); int currentItem = mViewPager.getCurrentItem (); currentItem++; mViewPager.setCurrentItem (currentItem); postDelayed (this, 2000);}} public void start () {if (! isRun) {removeCallbacks (this); isRun = true; postDelayed (this, 2000) }} public void stop () {if (isRun) {isRun = false; removeCallbacks (this);} at this point, I believe you have a deeper understanding of "how to achieve picture rotation in Android development". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report