Android简单实现右侧字符快速滑动

步骤:

1、自定义一个view继承自View,构造中初始化画笔。

2、在onDraw中计算每个字符的位置并从上往下画出所有字符

3、在onTouch中计算手指触摸到的位置并取出该位置的字符

4、通过接口回调把字符回传给调用处


自定义View代码:

package com.jingzx.myviewpj;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Function:
 * Created by TianMing.Xiong on 2017/9/11.
 */

public class LeterView extends View {
    public static final String[] LETERS = {"A","B","C","D","E","F","G","H","I","J","K","L","N"};
    private static final String TAG = LeterView.class.getSimpleName();
    private int textLength;
    private Paint paint;
    private boolean isShow;

    public LeterView(Context context) {
        super(context);
        init();
    }

    public LeterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        textLength = LETERS.length ;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(25f);
        paint.setColor(Color.RED);
        this.setBackgroundColor(Color.GRAY);
    }

    public LeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(isShow) canvas.drawColor(Color.BLUE);
        drawLeterText(canvas);
    }

    private void drawLeterText(Canvas canvas) {
        float tmp = getHeight()/textLength;
        for(int i=0;i<textLength;i++){
            String leter = LETERS[i];
            float x = getWidth()/2-paint.measureText(leter)/2;
            float y = getHeight()/textLength*i + tmp;
            canvas.drawText(leter,x,y,paint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        isShow = true ;
        int positionY = (int) (event.getY())/(getHeight()/textLength);
         if(isShow && positionY>=0 && positionY<LETERS.length){
             if(null!=iShowLeters){
                 iShowLeters.isTouch(isShow);
                 iShowLeters.showLeters(LETERS[positionY]);
             }
         }

         if(event.getAction()==MotionEvent.ACTION_UP){
             isShow = false ;
             if(null!=iShowLeters){
                 iShowLeters.isTouch(isShow);
             }
         }
        postInvalidate();
        return true;

    }

    //把选中的字符回调给调用处
    IShowLeters iShowLeters ;

    public void setiShowLeters(IShowLeters iShowLeters) {
        this.iShowLeters = iShowLeters;
    }

    interface IShowLeters{
        void showLeters(String text);
        void isTouch(boolean isTouch);
    }
}

显示布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jingzx.myviewpj.MainActivity">

    <TextView
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:id="@+id/tv_leter"
        android:gravity="center"
        android:background="#b39696"
        android:layout_width="90dp"
        android:layout_height="90dp"
        />
    <com.jingzx.myviewpj.LeterView
        android:id="@+id/cust_view_leters"
        android:layout_alignParentRight="true"
        android:layout_width="30dp"
        android:layout_height="match_parent"/>

</RelativeLayout>

调用处代码:

package com.jingzx.myviewpj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LeterView.IShowLeters{

    private LeterView leterView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        initView();
        initCallBack();
    }

    private void initCallBack() {
        leterView.setiShowLeters(this);
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv_leter);
        leterView = (LeterView) findViewById(R.id.cust_view_leters);
    }

    @Override
    public void showLeters(String text) {
        textView.setText(text);
    }

    @Override
    public void isTouch(boolean isTouch) {
        if(isTouch){
            textView.setVisibility(View.VISIBLE);
        }else{
            textView.setVisibility(View.GONE) ;
        }
    }
}

最后效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值