android 横向滚动屏幕实现(3)
16lz
2021-01-23
这次主要是使用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>
源码下载: 点击打开链接
更多相关文章
- android imageswitcher gallery 根据数据库内图片名字进行查看/
- Android OkHttp3 上传多张图片
- Android屏幕旋转时Activity不重新调用onCreate的方法
- Android中TextView中加图片,超链接,一部分字或者背景变色。不断补
- 关于Android设备屏幕大小及密度的系统参数类
- flutter包名、应用名称、图标、启动图片修改
- iphone/android比较学习之──获取屏幕分辨率
- 【摘录】 Android屏幕元素层次结构
- Android程序优化之对屏幕旋转的处理总结