日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

28自定义View 模仿联系人字母侧栏

發布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 28自定义View 模仿联系人字母侧栏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自定義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;//指定一個位置  不在索引范圍內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);}/*** 進行繪制* @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;//設置文字顏色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;//獲取你當前索引位置pressedPosition = (int)(eventY/singleHeight);//顯示提示的VIew  設置內容if(mSelectView!=null){if(pressedPosition>=0&&pressedPosition<LETTES.length){mSelectView.setVisibility(View.VISIBLE);mSelectView.setText(LETTES[pressedPosition]);}}invalidate();//刷新break;case  MotionEvent.ACTION_UP://抬起//還原當前位置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>

轉載于:https://www.cnblogs.com/muyuge/p/6152165.html

總結

以上是生活随笔為你收集整理的28自定义View 模仿联系人字母侧栏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。