Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng)
今天給大家實(shí)現(xiàn)的功能是類似于ViewFlipper的圖片滑動(dòng)的效果,供大家參考,具體內(nèi)容如下
現(xiàn)在就直接上代碼吧!
代碼實(shí)例:
1、xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" ? ? android:layout_height="match_parent" tools:context="com.zking.laci.android20_shou.MainActivity"> ? ? <ViewFlipper ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/vf_main_image" ? ? ? ? ></ViewFlipper> </LinearLayout>
2、activity類
package com.zking.laci.android20_shou;
?
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
?
public class MainActivity extends AppCompatActivity{
?
? ? private ViewFlipper vf_main_image;
? ? private int images[]={R.drawable.s10,R.drawable.s1,R.drawable.s7};
? ? private GestureDetector ges;
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //得到控件
? ? ? ? vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image);
? ? ? ? for (int i = 0; i < images.length; i++) {
? ? ? ? ? ? ImageView iv=new ImageView(this);
? ? ? ? ? ? iv.setImageResource(images[i]);
? ? ? ? ? ? //將圖片防區(qū)ViewFlipper中
? ? ? ? ? ? vf_main_image.addView(iv);
? ? ? ? }
?
? ? ? ? //實(shí)例化一個(gè)手勢(shì)檢測(cè)器的類
? ? ? ? ges = new GestureDetector(this, new GestureDetector.OnGestureListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 按下
? ? ? ? ? ? ?*/
? ? ? ? ? ? public boolean onDown(MotionEvent e) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 按下但是沒(méi)有抬起
? ? ? ? ? ? ?* @param e
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onShowPress(MotionEvent e) {
?
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 按一下 短按
? ? ? ? ? ? ?* @param e
? ? ? ? ? ? ?* @return
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onSingleTapUp(MotionEvent e) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 滑動(dòng)
? ? ? ? ? ? ?* @param e1
? ? ? ? ? ? ?* @param e2
? ? ? ? ? ? ?* @param distanceX
? ? ? ? ? ? ?* @param distanceY
? ? ? ? ? ? ?* @return
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 長(zhǎng)按
? ? ? ? ? ? ?* @param e
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onLongPress(MotionEvent e) {
?
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 拖動(dòng)
? ? ? ? ? ? ?* @param e1 ? ?起點(diǎn)
? ? ? ? ? ? ?* @param e2 ? ?終點(diǎn)
? ? ? ? ? ? ?* @param velocityX x值
? ? ? ? ? ? ?* @param velocityY ? ?Y值
? ? ? ? ? ? ?* @return
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
? ? ? ? ? ? ? ? if(e1.getX()-e2.getX()>200){
? ? ? ? ? ? ? ? ? ? //顯示下一張
? ? ? ? ? ? ? ? ? ? vf_main_image.showNext();
? ? ? ? ? ? ? ? ? ? //設(shè)置出去的動(dòng)畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setOutAnimation(MainActivity.this,R.anim.left_out);
? ? ? ? ? ? ? ? ? ? //設(shè)置進(jìn)來(lái)的動(dòng)畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setInAnimation(MainActivity.this,R.anim.right_in);
? ? ? ? ? ? ? ? }else if(e2.getX()-e1.getX()>200){
? ? ? ? ? ? ? ? ? ? //顯示上一張
? ? ? ? ? ? ? ? ? ? vf_main_image.showPrevious();
? ? ? ? ? ? ? ? ? ? //設(shè)置出去的動(dòng)畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out);
? ? ? ? ? ? ? ? ? ? //設(shè)置進(jìn)來(lái)的動(dòng)畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setInAnimation(MainActivity.this,R.anim.left_in);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? });
?
? ? }
?
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? //讓觸摸繼續(xù)傳遞下去
? ? ? ? return ges.onTouchEvent(event);
? ? }
}3、進(jìn)入和出去的四個(gè)動(dòng)畫
left_out:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="-100%p" ? ? ? ? android:fromYDelta="0" ? ? ? ? android:toYDelta="-100%p" ? ? ? ? ></translate> ? </set>
right_in:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="100%p" ? ? ? ? android:toXDelta="0" ? ? ? ? android:fromYDelta="-100%p" ? ? ? ? android:toYDelta="0" ? ? ? ? ></translate> ? </set>
right_out:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="100%p" ? ? ? ? android:fromYDelta="0" ? ? ? ? android:toYDelta="-100%p" ? ? ? ? ></translate> ? </set>
left_in:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="-100%p" ? ? ? ? android:toXDelta="0" ? ? ? ? android:fromYDelta="-100%p" ? ? ? ? android:toYDelta="0" ? ? ? ? ></translate> ? </set>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android控件RefreshableView實(shí)現(xiàn)下拉刷新
這篇文章主要為大家詳細(xì)介紹了Android控件RefreshableView實(shí)現(xiàn)下拉刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android實(shí)現(xiàn)左滑退出Activity的完美封裝
這篇文章主要介紹了Android實(shí)現(xiàn)左滑退出Activity的完美封裝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Android實(shí)現(xiàn)讀取相機(jī)(相冊(cè))圖片并進(jìn)行剪裁
在 Android應(yīng)用中,很多時(shí)候我們需要實(shí)現(xiàn)上傳圖片,或者直接調(diào)用手機(jī)上的拍照功能拍照處理然后直接顯示并上傳功能,下面將講述調(diào)用相機(jī)拍照處理圖片然后顯示和調(diào)用手機(jī)相冊(cè)中的圖片處理然后顯示的功能2015-08-08
Android開(kāi)發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn)
這篇文章主要介紹了Android開(kāi)發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

