Letter Sorting on the Right of Android

Source: Internet
Author: User

Import Android. content. context;

Import Android. Graphics. Canvas;

Import Android. Graphics. color;

Import Android. Graphics. paint;

Import Android. Graphics. typeface;

Import Android. util. attributeset;

Import Android. View. motionevent;

Import Android. View. view;

 

Public class rightletterview extends view {

String [] letters = {"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 "};

Paint paint = new paint ();

Ontouchingletterchangedlistener;

Boolean showbkg = false;

Int choose =-1;

Int letterslength;

 

Public rightletterview (context ){

Super (context );

}

 

Public rightletterview (context arg0, attributeset arg1, int arg2 ){

Super (arg0, arg1, arg2 );

}

 

Public rightletterview (context, attributeset attrs ){

Super (context, attrs );

}

 

@ Override

Protected void onattachedtowindow (){

// Todo auto-generated method stub

Super. onattachedtowindow ();

Letterslength = letters. length;

}

 

@ Override

Protected void ondraw (canvas ){

Super. ondraw (canvas );

If (showbkg ){

Canvas. drawcolor (color. parsecolor ("#10000000 "));//
Clear and black background

}

 

Int Height = getheight ();

Int width = getwidth ();

Int singleheight = height/letterslength;

 

For (INT I = 0; I <letterslength; I ++ ){

Paint. setcolor (color. Black );

Paint. settypeface (typeface. default_bold );

Paint. setantialias (true );//
Smooth

Paint. settextsize (50366f );

If (I = choose ){

Paint. setcolor (color. parsecolor ("# 3399ff "));

Paint. setfakeboldtext (true );

}

Float xpos = width/2-paint. measuretext (letters [I])/2;

Float ypos = singleheight * (I + 1 );

Canvas. drawtext (letters [I], xpos, ypos, paint );

Paint. Reset ();

}

 

}

 

@ Override

Public Boolean dispatchtouchevent (motionevent event ){

Final int action = event. getaction ();

Final float y = event. Gety ();

Final int oldchoose = choose;

Final ontouchingletterchangedlistener listener = ontouchingletterchangedlistener;

Final int c = (INT) (y/getheight () * letterslength );

 

Switch (Action ){

Case motionevent. action_down:

Showbkg = true;

If (oldchoose! = C & listener! = NULL ){

If (C> 0 & C <= letterslength ){

Listener. ontouchingletterchanged (letters [c]);

Choose = C;

Invalidate ();

}

}

Break;

Case motionevent. action_move:

If (oldchoose! = C & listener! = NULL ){

If (C> 0 & C <= letterslength ){

Listener. ontouchingletterchanged (letters [c]);

Choose = C;

Invalidate ();

}

}

Break;

Case motionevent. action_up:

Showbkg = false;

Choose =-1;

Invalidate ();

Break;

 

Default:

Break;

}

 

Return true;

}

 

@ Override

Public Boolean ontouchevent (motionevent arg0 ){

Return super. ontouchevent (arg0 );

}

 

Public void setontouchingletterchangedlistener (

Ontouchingletterchangedlistener ){

This. ontouchingletterchangedlistener = ontouchingletterchangedlistener;

}

 

Public interface ontouchingletterchangedlistener {

Public void ontouchingletterchanged (string S );

}

 

Public void setletters (string [] letters ){

This. Letters = letters;

}

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.