用ViewFlipper来模仿UC的翻页效果,viewflipperuc
投稿于 被查看 45219 次 评论:253
用ViewFlipper来模仿UC的翻页效果,viewflipperuc
主要通过Xml布局来实现Android专业开发群1:150086842 Android专业开发群2:219277004
1.这个是主类
package com.webStudy; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; import android.widget.ViewFlipper; public class Activity01 extends Activity { private ViewFlipper mViewFlipper; private GestureDetector mGestureDetector; private Button button; private Button chinese; private String msg =null; private int id = 0; private Paint paint=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); //机器人按钮 chinese =(Button) this.findViewById(R.id.enterchinese); button =(Button) this.findViewById(R.id.Button_next2); mViewFlipper = (ViewFlipper) this.findViewById(R.id.details); mGestureDetector = new GestureDetector(this,new LearnGestureListener()); paint = new Paint(); //机器人按钮 button.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { if(id>=3){ id=0; } switch(id){ case 0: msg ="滑动屏幕可以进入其它板块"; break; case 1: msg = "欢迎使用本软件,感谢你的支持"; break; case 2: msg = "还有什么呢,想不到了"; break; } Toast toast =Toast.makeText(Activity01.this, msg, Toast.LENGTH_SHORT); toast.show(); id += 1; } }); chinese.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(Activity01.this,denglu.class); startActivity(intent); Activity01.this.finish(); } }); } public void onDrawShipe(Canvas canvas){ paint.setColor(Color.BLACK); canvas.drawText(msg, 50, 320, paint); } public boolean onTouchEvent(MotionEvent event){ if(mGestureDetector.onTouchEvent(event)) return true; else return false; } class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ if(e1.getX()>e2.getX()){ mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); mViewFlipper.setFlipInterval(1000); mViewFlipper.showNext(); } if(e1.getX()<e2.getX()){ mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in); mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out); mViewFlipper.showPrevious(); mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); } return true; } } public boolean onCreateOptionsMenu(Menu menu){ menu.add(Menu.NONE,1,1,"帮助").setIcon(android.R.drawable.ic_menu_help); menu.add(Menu.NONE,2,2,"退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel); return true; } public boolean onOptionsItemSelected(MenuItem id){ switch(id.getItemId()){ case 1: break; case 2: Dialog dialog = new AlertDialog.Builder(Activity01.this) .setTitle("你要退出此程序") .setPositiveButton("确定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int whichButton){ Activity01.this.finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create(); dialog.show(); break; } return false; } public boolean onPreparedOptionsMenu(Menu menu) { return true; } }
2.这个是布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ViewFlipper android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AbsoluteLayout android:orientation="vertical" android:background="@drawable/snack" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="" android:id="@+id/Button_next2" android:background="@drawable/log2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="260dp" android:layout_y="320dp" > </Button> </AbsoluteLayout> <LinearLayout android:orientation="vertical" android:background="@drawable/zhuye" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Next2" android:id="@+id/Button_next1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/image1" android:src="@drawable/icon" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ImageView> </LinearLayout> <AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/kaichangchinese" android:weightSum="1" android:orientation="vertical"> <TextView android:layout_width="0dip" android:layout_height="0dip" /> <TextView android:textSize="20dip" android:text=" 秦王嬴政一统天下后,卫庄率秦军攻打墨家机关城,铲除墨家剩余势力,为了保卫机关城,天明一行人来到机关城禁地,这是一个神秘的地方,需要经过重重考验,才能获得最强大的武器和力量,拯救机关城于危难之中,小朋友们,你们愿意帮助天明他们冲破重重关卡,取得最后的胜利吗?" android:textColor="#000000" android:id="@+id/textchinese" android:layout_width="276dp" android:layout_height="335dp" android:layout_x="26dp" android:layout_y="119dp"></TextView> <Button android:layout_width="72dp" android:background="@drawable/backchinese" android:id="@+id/backchinese" android:layout_height="67dp" android:layout_x="212dp" android:layout_y="389dp"></Button> <Button android:layout_width="93dp" android:background="@drawable/enterchinese" android:id="@+id/enterchinese" android:layout_height="67dp" android:layout_x="26dp" android:layout_y="386dp"></Button> </AbsoluteLayout> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/kaichang00math" android:weightSum="1" android:orientation="vertical"> <TextView android:layout_width="0dip" android:layout_height="0dip" /> <TextView android:textColor="#000000" android:textSize="20dip" android:text=" 记忆在时间中成风, 往事如流水匆匆逝去 , 生命花瓣在冬夜飘零 , 犹如停留叶面的晨露, 这样的动荡不安的世界, 没有人可以选择自己的命运 ,作为卫庄手下一流的杀手组织,聚散流沙中的每个人都是神秘冷酷的武林高手,想知道他们平时的生活都是什么样子的吗? 接下来就让我们一起来一探究竟吧!" android:id="@+id/text" android:layout_width="260dp" android:layout_x="31dp" android:layout_y="105dp" android:layout_height="318dp"> </TextView> <Button android:id="@+id/enter" android:layout_width="162dp" android:layout_height="123dp" android:background="@drawable/enter01math" android:layout_x="0dp" android:layout_y="380dp"></Button> <Button android:id="@+id/back" android:layout_width="92dp" android:layout_height="66dp" android:background="@drawable/backmath" android:layout_x="192dp" android:layout_y="407dp"></Button> </AbsoluteLayout> </ViewFlipper> </LinearLayout>
3.分别四个动画的实现
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /> </set>
4.[XML]代码
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" /> </set>
5.[XML]代码
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="500" /> </set>
6.[XML]代码
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="500" /> </set>
用户评论