Android horizontal scrolling screen implementation (3)

Source: Internet
Author: User

This time, we mainly used viewflipper, a built-in control of the Android system, to combine gestures and animations to achieve the scrolling effect, similar to the help effect of software.

:

Main Code:

Package COM. pioneersoft. systemhelp; import Java. util. hashmap; import Org. apache. HTTP. impl. client. tunnelrefusedexception; import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. bundle; import android. util. log; import android. view. motionevent; import android. view. view; import android. view. animation. animation; import android. view. animation. translateanimation; import android. widget. imageview; import android. widget. linearlayout; import android. widget. relativelayout; import android. widget. viewflipper; import android. widget. framelayout. layoutparams; public class systemhelpactivity extends activity {public final int next_anim = 2; public final int duration = 1000; private int width, height; private int index, startx, lastx; private context mcontext; viewflipper fliper; private animation attributes, attributes; private animation attributes, attributes; private bitmap normalpoint, selectpoint; private linearlayout bottomlayout; // Private relativelayout container; private imageview lastsel, currentsel; private imageview leftview, rightview; // Private int maxcount = 14; private int [] pics = {R. drawable. h0001, R. drawable. h0002, R. drawable. h0003, R. drawable. h0004, R. drawable. h0005, R. drawable. h0006 ,}; // Private hashmap <integer, integer> hasmap = new hashmap <integer, integer> ();/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); setview ();} private void setview () {mcontext = This; width = getresources (). getdisplaymetrics (). widthpixels; // Container = (relativelayout) findviewbyid (R. id. container); bottomlayout = (linearlayout) findviewbyid (R. id. bottombar); fliper = (viewflipper) findviewbyid (R. id. viewpaper); leftview = (imageview) findviewbyid (R. id. leftturn); rightview = (imageview) findviewbyid (R. id. rightturn); leftview. setonclicklistener (New turnmonitor (); rightview. setonclicklistener (New turnmonitor (); normalpoint = bitmapfactory. decoderesource (getresources (), R. drawable. page_off); selectpoint = bitmapfactory. decoderesource (getresources (), R. drawable. page_on); // Add the image to viewflipper. for (INT I = 0; I <pics. length; I ++) {imageview = new imageview (mcontext); imageview. setimageresource (PICS [I]); fliper. addview (imageview, I);} // Add the dot to linerlayout dynamically. for (Int J = 0; j <pics. length; j ++) {imageview = new imageview (mcontext); If (j = 0) {imageview. setimagebitmap (selectpoint);} else {imageview. setimagebitmap (normalpoint);} // relativelayout. layoutparams Params = new relativelayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); bottomlayout. addview (imageview);} mlefttorightin = new translateanimation (-width, 0, 0, 0); mlefttorightin. setduration (duration); mlefttorightout = new translateanimation (0, width, 0, 0); mlefttorightout. setduration (duration); mrighttoleftin = new translateanimation (width, 0, 0, 0); mrighttoleftin. setduration (duration); mrighttoleftout = new translateanimation (0,-width, 0, 0); mrighttoleftout. setduration (duration);} class turnmonitor implements view. onclicklistener {@ overridepublic void onclick (view v) {If (index <= 0) {isrightenable = false; isleftenable = true;} else if (index> = pics. length-1) {isrightenable = true; isleftenable = false;} else {isrightenable = true; isleftenable = true;} If (V = leftview) {If (isrightenable) {// left to rightfliper. setinanimation (mlefttorightin); fliper. setoutanimation (mlefttorightout); fliper. showprevious (); lastanim (); index --; nextanim () ;}} else if (V = rightview) {If (isleftenable) {// right to leftfliper. setinanimation (mrighttoleftin); fliper. setoutanimation (mrighttoleftout); fliper. shownext (); lastanim (); index ++; nextanim () ;}}} private Boolean isleftenable = true; private Boolean isrightenable = true; @ overridepublic Boolean ontouchevent (motionevent) {int action = event. getaction (); Switch (Action) {Case motionevent. action_down: startx = (INT) event. getx (); break; Case motionevent. action_up: lastx = (INT) event. getx (); If (index <= 0) {isrightenable = false; isleftenable = true;} else if (index> = pics. length-1) {isrightenable = true; isleftenable = false;} else {isrightenable = true; isleftenable = true;} If (isrightenable & lastx-startx> 120) {// left to rightfliper. setinanimation (mlefttorightin); fliper. setoutanimation (mlefttorightout); fliper. showprevious (); lastanim (); index --; nextanim ();} else if (isleftenable & lastx-startx <-120) {// right to leftfliper. setinanimation (mrighttoleftin); fliper. setoutanimation (mrighttoleftout); fliper. shownext (); lastanim (); index ++; nextanim () ;}break; Case motionevent. action_move: break;} return Super. ontouchevent (event);} private void lastanim () {// log. E ("MSG", "lastindex" + index); lastsel = (imageview) bottomlayout. getchildat (INDEX);} private void nextanim () {// log. E ("MSG", "curindex" + index); currentsel = (imageview) bottomlayout. getchildat (INDEX); currentsel. setimagebitmap (selectpoint); lastsel. setimagebitmap (normalpoint );}}

Package COM. pioneersoft. systemhelp; import Java. security. ACL. lastownerexception; import Java. util. arraylist; import Java. util. list; import android. content. context; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android.net. uri; import android. OS. handler; import android. OS. message; import android. provider. mediastore; import android. util. attributeset; import android. util. log; import android. widget. framelayout; import android. widget. imageview; import android. widget. linearlayout; import android. widget. relativelayout; import android. widget. viewflipper; import android. view. gesturedetector; import android. view. gravity; import android. view. motionevent; import android. view. gesturedetector. ongesturelistener; import android. view. view; import android. view. view. ontouchlistener; import android. view. animation. animation; import android. view. animation. translateanimation; public class scanview extends framelayout {public final int next_anim = 2; public final int duration = 1000; private int width, height; private int index, startx, lastx; private context mcontext; viewflipper fliper; private animation attributes, attributes; private animation mrighttoleftin, attributes; private bitmap normalpoint, selectpoint; private linearlayout layout; private imageview lastsel, currentsel; private int [] pics = {R. drawable. h0001, R. drawable. h0002, R. drawable. h0003, R. drawable. h0004, R. drawable. h0005, R. drawable. h0006, R. drawable. h0007, R. drawable. h0008, R. drawable. h0009, R. drawable. h0010, R. drawable. h0011, R. drawable. h0012, R. drawable. h0013, R. drawable. h0014}; Public scanview (context, attributeset attrs) {super (context, attrs); setview () ;}public scanview (context c) {super (C ); setview ();} private void setview () {mcontext = getcontext (); width = getresources (). getdisplaymetrics (). widthpixels; fliper = new viewflipper (mcontext); layout = new linearlayout (mcontext); layout. setgravity (gravity. bottom | gravity. center_horizontal); normalpoint = bitmapfactory. decoderesource (getresources (), R. drawable. page_off); selectpoint = bitmapfactory. decoderesource (getresources (), R. drawable. page_on); // Add the image to viewflipper. for (INT I = 0; I <pics. length; I ++) {imageview = new imageview (mcontext); imageview. setimageresource (PICS [I]); fliper. addview (imageview);} // Add the dot to linerlayout dynamically. for (Int J = 0; j <pics. length; j ++) {imageview = new imageview (mcontext); If (j = 0) {imageview. setimagebitmap (selectpoint);} else {imageview. setimagebitmap (normalpoint);} relativelayout. layoutparams Params = new relativelayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); layout. addview (imageview, Params);} addview (fliper); addview (layout); mlefttorightin = new translateanimation (-width, 0, 0, 0); mlefttorightin. setduration (duration); mlefttorightout = new translateanimation (0, width, 0, 0); mlefttorightout. setduration (duration); mrighttoleftin = new translateanimation (width, 0, 0, 0); mrighttoleftin. setduration (duration); mrighttoleftout = new translateanimation (0,-width, 0, 0); mrighttoleftout. setduration (duration);} private Boolean isleftenable = false; private Boolean isrightenable = true; @ overridepublic Boolean ontouchevent (motionevent event) {int action = event. getaction (); Switch (Action) {Case motionevent. action_down: startx = (INT) event. getx (); break; Case motionevent. action_up: lastx = (INT) event. getx (); If (index <= 0) {isrightenable = true; isleftenable = false;} else if (index> = pics. length-1) {isrightenable = false; isleftenable = true;} If (isrightenable & amp; lastx-startx> 120) {// left to rightfliper. setinanimation (mlefttorightin); fliper. setoutanimation (mlefttorightout); fliper. shownext (); lastanim (); index --; nextanim ();} else if (isleftenable & lastx-startx <-120) {// right to leftfliper. setinanimation (mrighttoleftin); fliper. setoutanimation (mrighttoleftout); fliper. showprevious (); lastanim (); index ++; nextanim ();} break; Case motionevent. action_move: break;} return true;} private void lastanim () {// log. E ("MSG", "lastindex" + index); lastsel = (imageview) layout. getchildat (INDEX);} private void nextanim () {// log. E ("MSG", "curindex" + index); currentsel = (imageview) layout. getchildat (INDEX); currentsel. setimagebitmap (selectpoint); lastsel. setimagebitmap (normalpoint );}}

Layout file:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/container"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ViewFlipper        android:id="@+id/viewPaper"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="center_vertical" >    </ViewFlipper>        <ImageView            android:id="@+id/leftTurn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_marginLeft="20dip"        android:layout_alignParentLeft="true"        android:background="@drawable/left"            />        <ImageView            android:id="@+id/rightTurn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_marginRight="20dip"        android:layout_alignParentRight="true"        android:background="@drawable/right"            />    <LinearLayout        android:id="@+id/bottomBar"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:gravity="center_horizontal"        android:layout_marginBottom="20dip"        android:layout_alignParentBottom="true"        /></RelativeLayout>

Download source code: Click the open link

Related Article

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.