28自定义View 模仿联系人字母侧栏
生活随笔
收集整理的這篇文章主要介紹了
28自定义View 模仿联系人字母侧栏
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
自定義View
LetterView.java
package com.qf.sxy.customview02;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;/*** Created by sxy on 2016/9/29.*/
public class LetterView extends View {// 索引的字母public static final String[] LETTES ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};private Paint mTextPaint;//指定一個位置 不在索引范圍內(nèi)private int pressedPosition = -1;//獲取展示的Viewprivate TextView mSelectView;public void setSelectTextView(TextView tv){this.mSelectView = tv;}public LetterView(Context context, AttributeSet attrs) {super(context, attrs);//初始化畫筆initPaint();}private void initPaint() {mTextPaint = new Paint();mTextPaint.setAntiAlias(true);mTextPaint.setColor(Color.RED);mTextPaint.setTextSize(30);}public LetterView(Context context) {this(context,null);}/*** 進(jìn)行繪制* @param canvas*/@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//獲取 寬和高int height = getHeight();int width = getWidth();//字符串序列的長度int lettersLength = LETTES.length;//獲取每個字符的高度int singleHeight = height/lettersLength;//開始繪制for(int i=0;i<lettersLength;i++){//獲取x的位置int xPos = (int)(width/2-mTextPaint.measureText(LETTES[i])/2);//獲取Y的位置int yPos = singleHeight+i*singleHeight;//設(shè)置文字顏色if(pressedPosition == i){mTextPaint.setColor(Color.BLUE);}else{mTextPaint.setColor(Color.BLACK);}//繪制文本canvas.drawText(LETTES[i],xPos,yPos,mTextPaint);}}/*** 觸摸事件** true 消費事件 false 不處理* @param event* @return*/@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()){case MotionEvent.ACTION_DOWN://按下case MotionEvent.ACTION_MOVE://移動//獲取手指的Y位置float eventY = event.getY();//每個字母單獨的高度int singleHeight = getMeasuredHeight()/LETTES.length;//獲取你當(dāng)前索引位置pressedPosition = (int)(eventY/singleHeight);//顯示提示的VIew 設(shè)置內(nèi)容if(mSelectView!=null){if(pressedPosition>=0&&pressedPosition<LETTES.length){mSelectView.setVisibility(View.VISIBLE);mSelectView.setText(LETTES[pressedPosition]);}}invalidate();//刷新break;case MotionEvent.ACTION_UP://抬起//還原當(dāng)前位置pressedPosition = -1;//隱藏提示的Viewif (mSelectView!=null){mSelectView.setVisibility(View.GONE);}invalidate();//刷新break;}return true;}
}
MainActivity.java
package com.qf.sxy.customview02;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv;private LetterView letterView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = ((TextView) findViewById(R.id.select_tv));letterView = ((LetterView) findViewById(R.id.letterView));//傳遞展示的ViewletterView.setSelectTextView(tv);}
}
activity_main.xml
<?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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.qf.sxy.customview02.MainActivity"><com.qf.sxy.customview02.LetterView
android:id="@+id/letterView"android:layout_width="40dp"android:layout_height="match_parent"android:layout_alignParentRight="true"/><TextView
android:id="@+id/select_tv"android:layout_width="60dp"android:layout_height="60dp"android:layout_centerInParent="true"android:gravity="center"android:background="#00ff00"android:textSize="30sp"android:visibility="gone"android:text="A"/>
</RelativeLayout>
轉(zhuǎn)載于:https://www.cnblogs.com/muyuge/p/6152165.html
總結(jié)
以上是生活随笔為你收集整理的28自定义View 模仿联系人字母侧栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个歇斯底里的个性签名!
- 下一篇: 从技术细节看美团的架构