Android custom View identifies the currently selected control

Source: Internet
Author: User
Tags getcolor

One value is the code posted by an online friend. It is very convenient and you can learn a lot. this time I also posted some simple little things. I don't really want to describe graph-based code...
 
Subject Code
 
Myview code
Package com. suncco. taoxie;
 
Import android. content. Context;
Import android. content. res. TypedArray;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. View;
 
Public class MyView extends View {
Private int count;
Private float space, radii;
Private int point_normal_color, point_seleted_color;
 
// Select
Private int selected = 0;
 
// Background seleted normal
 
Public MyView (Context context, AttributeSet attrs ){
Super (context, attrs );
TypedArray a = context
. ObtainStyledAttributes (attrs, R. styleable. MyView );
 
Count = a. getInteger (R. styleable. MyView_count, 3 );
Space = a. getDimension (R. styleable. MyView_space, 9 );
Radii = a. getDimension (R. styleable. MyView_point_radii, 9 );
 
Point_normal_color = a. getColor (R. styleable. MyView_point_normal_color,
0x000000 );
Point_seleted_color = a. getColor (
R. styleable. MyView_point_seleted_color, 0xffff07 );
 
Int sum = attrs. getAttributeCount ();
A. recycle ();
}
 
Public void setCount (int count ){
This. count = count;
Invalidate ();
}
 
Public void next (){
If (selected <count-1)
Selected ++;
Else
Selected = 0;
Invalidate ();
}
 
Public void previous (){
If (selected> 0)
Selected --;
Else
Selected = count-1;
Invalidate ();
}
 
@ Override
Protected void onDraw (Canvas canvas ){
Paint paint = new Paint ();
Paint. setAntiAlias (true );
// Start position to center the whole
Float w = canvas. getWidth ()-(count * 2 * radii)-space * (count-1 );
 
For (int I = 0; I <count; I ++ ){
If (I = selected)
Paint. setColor (point_seleted_color );
Else
Paint. setColor (point_normal_color );
Canvas. drawCircle (w/2.f + radii + I * (space + radii ),
Radii + 1, (int) radii + 2)/2, paint );
}
}
 
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Super. onMeasure (widthMeasureSpec, heightMeasureSpec );
SetMeasuredDimension (widthMeasureSpec, (int) (radii * 2) + 2 );
}
 
Public void setSelected (int selectedId ){
If (selectedId> = 0 & selectedId <= count)
This. selected = selectedId;
Else if (selectedId <0)
This. selected = 0;
Else if (selectedId> count)
This. selected = count;
Invalidate ();
}
 
}
 
Custom namespace of the custom View:
The following custom attributes, such as count: total number, space: distance of each point, and so on, are commonly obtained...
 
The namespace is also called myview... code.
*** _ Attrs. xml of this file put in value. For example, I will go to haowuliaoa_attrs.xml here.
The namespace is also called myview... code.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "MyView">
<Attr name = "count" format = "integer"/>
<Attr name = "space" format = "dimension"/>
<Attr name = "point_size" format = "dimension"/>
<Attr name = "point_seleted_color" format = "color | reference"/>
<Attr name = "point_normal_color" format = "color | reference"/>
<Attr name = "point_radii" format = "dimension"/>
</Declare-styleable>
</Resources>
 
 
Everything is okay. You can apply it directly on the xml layout...
 
 
Xml Code
First, add your own namespace in the layout header.
Xmlns: haowuliaoa = "Name of the package at http://schemas.android.com/apk/res"
Xml Code
Then there is the xml layout...
Xml Code
<Package name. MyView android: id = "@ + id/myView"
Android: layout_width = "fill_parent" android: layout_height = "10dip"
Android: background = "#00000000" android: gravity = "center"
Android: layout_marginBottom = "4dip" suncco: count = "6" suncco: space = "10dip"
Suncco: point_size = "4dip" suncco: point_seleted_color = "# ff0000"
Suncco: point_normal_color = "# ffffff" suncco: point_radii = "5dip"/>
 
So is simple ....
The smaller points in the image are the results of today.
Functions are often used...

Author "whyhappy"
 

Related Article

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.