Android custom View imitation of WeChat LetterView effect
I won't say much nonsense, the specific code is as follows:
public class LetterView extends View { private String TAG = (); //A,B,C....Z,# public List<String> letters; private Paint mPaint; private int selectPosition = -1; private TextView mLetter; public void setmLetter(TextView mLetter) { = mLetter; } public LetterView(Context context) { this(context,null); } public LetterView(Context context, AttributeSet attrs) { this(context, attrs,0); } public LetterView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //Produce letters letters = new ArrayList<>(); for (int i = 65; i < 91; i++) { ((,"%c",i)); (TAG, "LetterView: "+ (,"%c",i)); } ("#");//Add a # //Initialize the brush mPaint = new Paint(); (true); (); (30); } @Override protected void onDraw(Canvas canvas) { (canvas); /** * Get the width of the View * Get the height of the View */ int width = getMeasuredWidth(); int height = getMeasuredHeight(); //Measure the width of the word int size = (); for (int i = 0; i < size; i++) { float textWidth = ((i)); int singleHeight = height / size; if (selectPosition == i){//Selected (); }else{ (); } ((i),(width - textWidth)/2, singleHeight * (i + 1),mPaint); /** * drawText() x y \_ is the baseline */ invalidate(); } } /** * Android encapsulates touch events, wraps actions and location information; onClick is also a motionEvent * onClick event is actually onTouchEvent event * @param event * @return */ @Override public boolean onTouchEvent(MotionEvent event) { //Packed action, location information// ();//Coordinate value relative to the View itself// ();//Return is the coordinate value relative to the screen float y = (); (TAG, "onTouchEvent: Y:"+ y); // (TAG, "onTouchEvent: RawY:"+() ); int measuredHeight = getMeasuredHeight(); int singleHeight = measuredHeight / (); int position = (int) (y / singleHeight); (TAG, "onTouchEvent: " + position ); switch (()) { case MotionEvent.ACTION_MOVE: (TAG, "onTouchEvent: ACTION_MOVE" ); case MotionEvent.ACTION_DOWN: (TAG, "onTouchEvent: ACTION_DOWN" ); selectPosition = position; if (mLetter != null) { (); //The limit situation may be out of bounds, so you need to judge if (position < () && position >= 0){ ((position)); } } break; case MotionEvent.ACTION_UP: (TAG, "onTouchEvent: ACTION_UP" ); selectPosition = -1; if (mLetter != null) { (); } break; } //Return true means the event has been processed return true; } }
Related Articles
5 methods to perfectly solve the problem of android soft keyboard blocking input box
When developing Android APPs, we often encounter situations where the keyboard blocks the input box. We must first put the keyboard away and then get the focus of the input box below. This way, the user experience is also very bad. Today, we will introduce to you 5 methods to perfectly solve the problem of blocking the input box on the Android keyboard.2018-03-03Solution to Android sliding conflict problem
This article mainly introduces in detail the solution to Android sliding conflict problem, which has certain reference value. Interested friends can refer to it.2017-02-02Detailed explanation of the ideas of Android to realize the big challenge of submarines in TikTok Games
"Submarine Challenge" is a mini-game on Douyin. It has been very popular recently and many friends have played it. Next, through this article, I will share with you the ideas of the Android mobile game submarine challenge. Friends who need it can refer to it.2020-04-04Android transparent and immersive status bar practice and source code analysis
This article mainly introduces Android's transparent and immersive status bar practice and source code analysis, which has certain reference value. Interested friends can refer to it.2017-03-03Android development method to prohibit drop-down notification bar
This article mainly introduces the method of prohibiting the drop-down notification bar in Android development. It analyzes the corresponding setting skills of Android permission control and Activity. Friends who need it can refer to it.2016-01-01Very practical small features Updated instance of Android application version
This article mainly introduces a very practical small function in detail, an update example of Android application version. Interested friends can refer to it.2016-08-08The start and stop of Android countdown The display of the remaining hours, minutes and seconds
This article mainly introduces the start and stop of Android countdown, and the display of the remaining hours, minutes and seconds. The sample code in the article is introduced in detail and has a certain reference value. Interested friends can refer to it.2019-09-09Android implements cool top bar
This article mainly introduces the cool top bar of Android implementation, which has certain reference value. Interested friends can refer to it.2016-09-09Android imitation QQ personal homepage pull-down rebound effect
This article mainly introduces the relevant information on the pull-down rebound effect of android imitation QQ personal homepage. The sample code in the article is introduced in detail and has a certain reference value. Interested friends can refer to it.2017-02-02An example of the Intent jump tool class implemented by Android development
This article mainly introduces the Intent jump tool class implemented by Android development, briefly describes the functions of the Intent component and provides related operation techniques such as page jump, taking photos, and picture calls in combination with examples. Friends who need it can refer to it.2017-11-11