android 橫向滾動螢幕實現(3)

來源:互聯網
上載者:User

這次主要是使用android系統內建控制項ViewFlipper 結合手勢和動畫實現滾動效果,類似於軟體協助效果

主要代碼:

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 mLeftToRightin,mLeftToRightOut;private Animation mRightTOLeftin,mRightToLeftOut;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);//將圖片加入ViewFlipper.for(int i = 0; i < pics.length; i++){ImageView imageView = new ImageView(mContext);imageView.setImageResource(pics[i]);fliper.addView(imageView,i);}//將點點動態加入Linerlayout.for(int j = 0; j < pics.length; j++){ImageView 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 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=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 mLeftToRightin,mLeftToRightOut;private Animation mRightTOLeftin,mRightToLeftOut;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 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);//將圖片加入ViewFlipper.for(int i = 0; i < pics.length; i++){ImageView imageView = new ImageView(mContext);imageView.setImageResource(pics[i]);fliper.addView(imageView);}//將點點動態加入Linerlayout.for(int j = 0; j < pics.length; j++){ImageView 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&&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);}}

布局檔案:

<?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>

源碼下載:點擊開啟連結

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.