android图片随手势左右滑动

代码:

图片手势左右滑动

package com.sl.quwei.ui.manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.*;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.sl.quwei.QuweiApplication;
import com.sl.quwei.R;

import static android.view.KeyEvent.KEYCODE_BACK;

/**
 * Created by IntelliJ IDEA.
 * User: wulong
 * Date: 12-2-22
 * Time: 上午4:15
 * To change this template use File | Settings | File Templates.
 */
public class AggHelp extends Activity implements GestureDetector.OnGestureListener {
    private static final String TAG = "Quwei.AggHelp";
    private int mScreenWidth = 240;
    private int mScreenHeight = 320;
    private GestureDetector detector;
    private ViewFlipper flipper;
    private int mIndexHelpPic = 0;
    private int[] mBgList = {
            R.drawable.agg_help01,        //滑动需要的几张图片
            R.drawable.agg_help02,
            R.drawable.agg_help03,
            R.drawable.agg_help04,
            R.drawable.agg_help05,
            R.drawable.agg_help06
    };

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agg_bangzhuzhiyin);

        CharSequence title = getTitle();
        String loginName = ((QuweiApplication) getApplication()).getLoginUserName();
        String titleName = AggTools.getNewTitle(title, loginName);
        setTitle(titleName);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
        if (mScreenWidth < 240) {
            mScreenWidth = 240;
        }

        if (mScreenHeight < 320) {
            mScreenHeight = 320;
        }


        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);

//        flipper.addView(addButtonByText("按钮"), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        flipper.addView(addImageByID(mBgList[mIndexHelpPic]), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        detector = new GestureDetector(this);
    }

    private void backToMainDesk() {
        //startActivity(new Intent(this, AggManage.class));
        finish();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KEYCODE_BACK:
                backToMainDesk();
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private View addImageByID(int id) {
        ImageView img = new ImageView(this);
        img.setImageResource(id);
        img.setAdjustViewBounds(true);
        img.setMaxWidth(mScreenWidth);
        img.setMaxHeight(mScreenHeight);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        return img;
    }

    public View addButtonByText(String text) {
        Button btn = new Button(this);
        btn.setText(text);
        return btn;
    }

    public View addTextByText(String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setGravity(1);
        return tv;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("Fling", "Activity onTouchEvent!");
        return this.detector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 监听滑动
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        // TODO Auto-generated method stub
        Log.i("Fling", "Fling Happened!");
        if (e1.getX() - e2.getX() > 120) {
            if (++mIndexHelpPic >= mBgList.length) {
                mIndexHelpPic = mBgList.length - 1;
                backToMainDesk();
                return true;
            }

            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
//            this.flipper.addView(addTextByText("文本框"), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            flipper.addView(addImageByID(mBgList[mIndexHelpPic]), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            this.flipper.showNext();
            return true;
        } else if (e1.getX() - e2.getX() < -120) {
            if (--mIndexHelpPic < 0) {
                mIndexHelpPic = 0;
                return true;
            }

            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
            this.flipper.showPrevious();
            return true;
        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

配置文件:

agg_bangzhuzhiyin.xml

<?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"
              android:background="@drawable/agg_sub_bg">
    <ViewFlipper android:id="@+id/ViewFlipper01"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
    </ViewFlipper>
</LinearLayout>

push_left_in.xml 放在anim

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

push_left_out.xml放在anim

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

push_right_in.xml放在anim

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

push_right_out.xml放在anim

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


实现了图片随手势左右滑动

你可能感兴趣的:(android,String,layout,float,button,encoding)