一个简单的滑动显示demo

之前无聊的时候写的一个滑动demo,用的是根据滑动距离计算位置重新布局的方式,在这里分享一下
话不多说,先上效果图

可能看上去有点卡顿,其实是我想表达拉到一点点,然后松开,可以直接恢复的效果

下面就是上代码了
基类
 

package cn.ALeeCJ.learningproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public abstract class BaseActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        init();
        addListener();
    }

    protected abstract void setContentView();

    protected abstract void init();

    protected abstract void addListener();

    protected void back(){
        finish();
    }

    @Override
    public void onClick(View v){}
}

实现类

package cn.ALeeCJ.learningproject.simple_slide_learning;

import android.os.Handler;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.ALeeCJ.learningproject.BaseActivity;
import cn.ALeeCJ.learningproject.R;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

public class SimpleSlideView extends BaseActivity implements View.OnTouchListener {

    private RelativeLayout rlFace;
    private TextView tv1;
    private TextView tvCos;
    private View vShade;
    private TextView tvYouAre;
    private RelativeLayout rlBase;
    private Handler mHandler;
    private float mLastY;
    private float maxBottomMargin;
    private List<String> cosList = new ArrayList<>();

    @Override
    protected void setContentView(){
        setContentView(R.layout.simple_slide_view);
    }

    @Override
    protected void init(){
        initCosList();
        ((TextView)findViewById(R.id.tvTitle)).setText("简单滑动测试");
        Button btnOpt = (Button) findViewById(R.id.btnOpt);
        btnOpt.setVisibility(View.VISIBLE);
        btnOpt.setText("退出");
        btnOpt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.ibBack).setVisibility(View.GONE);
        mHandler = new Handler();
        rlFace = (RelativeLayout) findViewById(R.id.rlFace);
        rlBase = (RelativeLayout) findViewById(R.id.rlBase);
        tv1 = (TextView) findViewById(R.id.tv1);
        tvCos = (TextView) findViewById(R.id.tvCos);
        vShade = findViewById(R.id.vShade);
        tvYouAre = (TextView) findViewById(R.id.tvYouAre);
        mHandler.postDelayed(shineRunnable, 500);
        mHandler.postDelayed(changeTextRunnable, 200);
        maxBottomMargin = getPxFromDp(370);
    }

    private void initCosList(){
        cosList.add("小米姑娘");
        cosList.add("三及第");
        cosList.add("七号餐厅");
        cosList.add("韬乐园");
        cosList.add("棒棒鸡");
        cosList.add("黄焖鸡");
        cosList.add("紫煲饭");
        cosList.add("兰州拉面");
        cosList.add("车仔面");
        cosList.add("新店");
    }

    private int getPxFromDp(int dp){
        float d = getResources().getDisplayMetrics().density;
        return (int)(d * dp + 0.5f);
    }

    private Runnable changeTextRunnable = new Runnable() {
        @Override
        public void run() {
            if(vShade.getVisibility() == View.GONE){
                tvCos.setText(cosList.get(getRandom()));
            }
            mHandler.postDelayed(this, 200);
        }
    };

    private int getRandom(){
        return new Random().nextInt(cosList.size() - 1);
    }

    private Runnable shineRunnable = new Runnable() {
        @Override
        public void run() {
            if(TextUtils.isEmpty(tv1.getText().toString().trim())){
                tv1.setText("上滑揭晓");
            }else{
                tv1.setText("");
            }
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected void addListener(){
        rlFace.setOnTouchListener(this);
        rlBase.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event){
        RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) rlFace.getLayoutParams();
        RelativeLayout.LayoutParams textlp = (RelativeLayout.LayoutParams) tvYouAre.getLayoutParams();
        int action = event.getAction();
        switch (action){
            case ACTION_DOWN:
                mLastY = event.getRawY();
                return true;
            case ACTION_MOVE:
                float moveY = event.getRawY() - mLastY;
                mLastY = event.getRawY();
                float finishY = rlp.bottomMargin - moveY;
                if(finishY < 0){
                    rlp.bottomMargin = 0;
                    rlp.topMargin = 0;
                    textlp.bottomMargin = 0;
                    textlp.topMargin = 0;
                    vShade.setVisibility(View.GONE);
                }else{
                    if(finishY > maxBottomMargin){
                        rlp.bottomMargin = (int)maxBottomMargin;
                        rlp.topMargin = (int)-maxBottomMargin;
                        textlp.bottomMargin = (int)-maxBottomMargin;
                        textlp.topMargin = (int)maxBottomMargin;
                        vShade.setVisibility(View.VISIBLE);
                    }else{
                        rlp.bottomMargin -= moveY;
                        rlp.topMargin += moveY;
                        textlp.bottomMargin += moveY;
                        textlp.topMargin -= moveY;
                        vShade.setVisibility(View.VISIBLE);
                    }
                }
                tvYouAre.requestLayout();
                rlFace.requestLayout();
                return true;
            case ACTION_UP:
                if(rlp.bottomMargin < (maxBottomMargin / 2)){
                    rlp.topMargin = 0;
                    rlp.bottomMargin = 0;
                    textlp.topMargin = 0;
                    textlp.bottomMargin = 0;
                    vShade.setVisibility(View.GONE);
                }else{
                    rlp.topMargin = (int)-maxBottomMargin;
                    rlp.bottomMargin = (int)maxBottomMargin;
                    textlp.bottomMargin = (int)-maxBottomMargin;
                    textlp.topMargin = (int)maxBottomMargin;
                    vShade.setVisibility(View.VISIBLE);
                }
                tvYouAre.requestLayout();
                rlFace.requestLayout();
                return true;
            default:
                return false;
        }
    }

    @Override
    protected void onDestroy(){
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/title_layout" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/rlBase"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/white">
            <TextView
                android:id="@+id/tvCos"
                android:layout_width="90dp"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="小米姑娘"
                android:textColor="@color/red5"
                android:textSize="65sp"
                android:layout_height="350dp"
                android:layout_margin="10dp" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/rlFace"
            android:clickable="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white">
            <TextView
                android:id="@+id/tv1"
                android:text="上滑揭晓"
                android:textColor="@color/green"
                android:textSize="20sp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_width="100dp"
                android:gravity="center"
                android:layout_height="50dp"
                android:layout_marginBottom="10dp"/>
            <View
                android:id="@+id/vShade"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/white"
                android:layout_alignParentBottom="true"/>

            <TextView
                android:id="@+id/tvYouAre"
                android:layout_above="@id/tv1"
                android:layout_width="90dp"
                android:layout_centerHorizontal="true"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="今天吃"
                android:paddingBottom="300dp"
                android:textSize="65sp"
                android:textColor="@color/blue5"/>
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

上面就是这个小demo的所有代码了,比较简单,就没写什么注释了

发布了24 篇原创文章 · 获赞 2 · 访问量 3658

猜你喜欢

转载自blog.csdn.net/ALee_130158/article/details/104700059
今日推荐