Simple ViewFlipper instance in Android

Source: Internet
Author: User

Simple ViewFlipper instance in Android

Main. xml <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;">

Main. java

package com.wty.mydemoviewflipper;import android.app.Activity;import android.os.Bundle;import com.wty.mydemoviewflipper.wiget.ViewFlipperView;public class Main extends Activity {    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new ViewFlipperView(this));    }}

IAdImages. java

Package com. wty. mydemoviewflipper. wiget; import com. wty. mydemoviewflipper. r;/*** image information interface * @ author vhreal **/public interface IAdImages {/*** advertisement image */int [] adImages = {R. drawable. img01, R. drawable. img02, R. drawable. img03, R. drawable. img04, R. drawable. img05, R. drawable. img06, R. drawable. img07, R. drawable. img08, R. drawable. img09, R. drawable. img10 };}

ViewFlipperView. java

Package com. wty. mydemoviewflipper. wiget; import android. content. context; import android. view. gravity; import android. view. motionEvent; import android. view. animation. animation; import android. view. animation. translateAnimation; import android. widget. frameLayout; import android. widget. imageView; import android. widget. linearLayout; import android. widget. viewFlipper; import com. wty. mydemoviewflipper. r;/*** This is a class It is similar to the wightet of ViewFlipper. * @ Author vhreal **/public class ViewFlipperView extends FrameLayout implements IAdImages {private Context context Context; // the caller's context private int currentAdImgIndex; // The current advertisement image index private Animation attributes; // enter the screen Animation private Animation left2RightOutAnimation from left to right; // enter the screen Animation private Animation right2LeftInAnimation from left to right; // enter the screen Animation private Animation right2LeftOutAnimation from right to left; // enter the screen Animation private int animationDuration = 300 from right to left; // animation takes 1000 milliseconds private ViewFlipper mViewFlipper; // sliding the page control private LinearLayout mTipLinearLayout; // the following point control private float startX = 0; // private float endX = 0 when touch action is down; // public ViewFlipperView (Context context) {super (context); this. context = context; setView ();}/*** display View */private void setView () {// initialize int screenWidth = getResources (). getDisplayMetrics (). widthPixels; mViewFlipper = new ViewFlipper (context); mTipLinearLayout = new LinearLayout (context); // initialize the animation watermark = new TranslateAnimation (-screenWidth, 0, 0, 0); trim. setDuration (animationDuration); left2RightOutAnimation = new TranslateAnimation (0, screenWidth, 0, 0); left2RightOutAnimation. setDuration (animationDuration); right2LeftInAnimation = new TranslateAnimation (screenWidth, 0, 0); right2LeftInAnimation. setDuration (animationDuration); right2LeftOutAnimation = new TranslateAnimation (0,-screenWidth, 0, 0); right2LeftOutAnimation. setDuration (animationDuration); // Add the ad image to ViewFlipper for (int I = 0; I 0 & endX> startX) {// view the previous page of the AD mViewFlipper. setInAnimation (left2RightInAnimation); mViewFlipper. setOutAnimation (left2RightOutAnimation); mViewFlipper. showPrevious (); currentAdImgIndex --; if (currentAdImgIndex <0) {currentAdImgIndex = 0 ;}} if (currentAdImgIndex <adImages. length-1 & endX <startX) {// view the advertisement mViewFlipper on the last page. setInAnimation (right2LeftInAnimation); mViewFlipper. setOutAnimation (right2LeftOutAnimation); mViewFlipper. showNext (); currentAdImgIndex ++; if (currentAdImgIndex> adImages. length-1) {currentAdImgIndex = adImages. length-1 ;}}// according to currentAdImgIndex, change the status of the bottom vertex ImageView currTipImageView = (ImageView) mTipLinearLayout. getChildAt (currentAdImgIndex); lastTipImageView. setImageResource (R. drawable. point_normal); currTipImageView. setImageResource (R. drawable. point_selected); break;} return true ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.