The Android GradientDrawable advanced application will not be used by the artist at all, gradientdrawable

Source: Internet
Author: User
Tags getcolor

The Android GradientDrawable advanced application will not be used by the artist at all, gradientdrawable

First, see Figure 1 for the custom Textview 2 and 3 for the click effect.

The specific implementation is as follows:

1. Define custom control attributes

<Declare-styleable name = "ShapeTextview">
<Attr name = "touchSolidColor" format = "color"/>
<Attr name = "solidColor" format = "color"/>
<Attr name = "cornesRadius" format = "dimension"/>
<Attr name = "topLeftRadius" format = "dimension"/>
<Attr name = "topRightRadius" format = "dimension"/>
<Attr name = "bottomLeftRadius" format = "dimension"/>
<Attr name = "bottomRightRadius" format = "dimension"/>
<Attr name = "stroke_Width" format = "dimension"/>
<Attr name = "stroke_Color" format = "color"/>
<Attr name = "strokeDashWidth" format = "dimension"/>
<Attr name = "strokeDashGap" format = "dimension"/>
<Attr name = "gradientStartColor" format = "color"/>
<Attr name = "gradientEndColor" format = "color"/>
<Attr name = "gradientCenterColor" format = "color"/>
<Attr name = "gradientUseLevel" format = "boolean"/>
<Attr name = "gradientAngle" format = "dimension"/>
<Attr name = "gradientOrientation">
<Enum name = "BL_TR" value = "0"/>
<Enum name = "BOTTOM_TOP" value = "1"/>
<Enum name = "BR_TL" value = "2"/>
<Enum name = "LEFT_RIGHT" value = "3"/>
<Enum name = "RIGHT_LEFT" value = "4"/>
<Enum name = "TL_BR" value = "5"/>
<Enum name = "TOP_BOTTOM" value = "6"/>
<Enum name = "TR_BL" value = "7"/>
</Attr>
<Attr name = "shapeType">
<Enum name = "LINEAR_GRADIENT" value = "0"/>
<Enum name = "OVAL" value = "1"/>
<Enum name = "LINE" value = "2"/>
<Enum name = "RING" value = "3"/>
</Attr>
<Attr name = "gradientType">
<Enum name = "linear" value = "0"/>
<Enum name = "radial" value = "1"/>
<Enum name = "sweep" value = "2"/>
</Attr>
<Attr name = "gradientRadius" format = "dimension"/>
</Declare-styleable>

 

2. Control Code

Package com. klower. component;

Import android. content. Context;
Import android. content. res. TypedArray;
Import android. graphics. Color;
Import android. graphics. drawable. GradientDrawable;
Import android. util. AttributeSet;
Import android. view. MotionEvent;
Import android. widget. TextView;

Import com. klower. R;

Public class ShapeTextView extends TextView {

Int solidColor, stroke_Color, gradientStartColor, gradientEndColor,
GradientCenterColor, touchColor;

Int cornesRadius, topLeftRadius, topRightRadius, bottomLeftRadius,
BottomRightRadius, stroke_Width, strokeDashWidth, strokeDashGap,
GradientAngle, gradientRadius, gradientType, gradientOrientation, shapeType;
Boolean gradientUseLevel;

GradientDrawable gradientDrawable;

Public ShapeTextView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}

Public ShapeTextView (Context context, AttributeSet attrs ){
Super (context, attrs );
InitData (context, attrs );
}

Public ShapeTextView (Context context ){
Super (context );
}

Private void initData (Context context, AttributeSet attrs ){
TypedArray a = context. obtainStyledAttributes (attrs,
R. styleable. ShapeTextview );
SolidColor = a. getColor (R. styleable. ShapeTextview_solidColor,
Color. TRANSPARENT );
Stroke_Color = a. getColor (R. styleable. ShapeTextview_stroke_Color,
Color. TRANSPARENT );
GradientStartColor =
. GetColor (R. styleable. ShapeTextview_gradientStartColor,
Color. TRANSPARENT );
GradientEndColor = a. getColor (
R. styleable. ShapeTextview_gradientEndColor, Color. TRANSPARENT );
GradientCenterColor = a. getColor (
R. styleable. ShapeTextview_gradientCenterColor,
Color. TRANSPARENT );
TouchColor = a. getColor (R. styleable. ShapeTextview_touchSolidColor,
Color. TRANSPARENT );

CornesRadius = (int) a. getDimension (
R. styleable. ShapeTextview_cornesRadius, 0 );
TopLeftRadius = (int) a. getDimension (
R. styleable. ShapeTextview_topLeftRadius, 0 );
TopRightRadius = (int) a. getDimension (
R. styleable. ShapeTextview_topRightRadius, 0 );
BottomLeftRadius = (int) a. getDimension (
R. styleable. ShapeTextview_bottomLeftRadius, 0 );
BottomRightRadius = (int) a. getDimension (
R. styleable. ShapeTextview_bottomRightRadius, 0 );
Stroke_Width = (int) a. getDimension (
R. styleable. ShapeTextview_stroke_Width, 0 );
StrokeDashWidth = (int) a. getDimension (
R. styleable. ShapeTextview_strokeDashWidth, 0 );
StrokeDashGap = (int) a. getDimension (
R. styleable. ShapeTextview_strokeDashGap, 0 );
GradientAngle = (int) a. getDimension (
R. styleable. ShapeTextview_gradientAngle, 0 );
GradientRadius = (int) a. getDimension (
R. styleable. ShapeTextview_gradientRadius, 0 );
GradientUseLevel = a. getBoolean (
R. styleable. ShapeTextview_gradientUseLevel, false );
GradientType = a. getInt (R. styleable. ShapeTextview_gradientType,-1 );
GradientOrientation = a. getInt (
R. styleable. ShapeTextview_gradientOrientation,-1 );
ShapeType = a. getInt (
R. styleable. ShapeTextview_shapeType,-1 );

GradientDrawable = new GradientDrawable ();
GradientDrawable. setStroke (stroke_Width, stroke_Color, strokeDashWidth,
StrokeDashGap );
// If Orientation is set, it is a gradient Button by default. Otherwise, it is a solid color Button.
If (gradientOrientation! =-1 ){
GradientDrawable
. SetOrientation (getOrientation (gradientOrientation ));
GradientDrawable. setColors (new int [] {gradientStartColor,
GradientCenterColor, gradientEndColor });
} Else {
GradientDrawable. setColor (solidColor );
}

If (shapeType! =-1 ){
GradientDrawable. setShape (shapeType );
}
// Whether it is a circle
If (shapeType! = GradientDrawable. OVAL ){
// If the specified Corner Radius is set, it is regarded as four buttons with the same angle; otherwise, four buttons with different angles are used.
If (cornesRadius! = 0 ){
GradientDrawable. setCornerRadius (cornesRadius );
} Else {
// Parameters 1 and 2 represent the upper left corner, 3 and 4 represent the upper right corner, 5 and 6 represent the lower right corner, and 7 and 8 represent the lower left corner
GradientDrawable. setCornerRadii (new float [] {topLeftRadius,
TopLeftRadius, topRightRadius, topRightRadius,
BottomRightRadius, bottomRightRadius, bottomLeftRadius,
BottomLeftRadius });
}
}

If (gradientUseLevel)
GradientDrawable. setUseLevel (gradientUseLevel );
If (gradientType! =-1)
GradientDrawable. setGradientType (gradientType );
GradientDrawable. setGradientRadius (gradientRadius );
SetBackground (gradientDrawable );


}

@ Override
Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN ){
If (touchColor! = Color. TRANSPARENT ){
GradientDrawable. setColor (touchColor );
SetBackground (gradientDrawable );
PostInvalidate ();
}
} Else if (event. getAction () = MotionEvent. ACTION_UP
| Event. getAction () = MotionEvent. ACTION_CANCEL ){
If (touchColor! = Color. TRANSPARENT ){
GradientDrawable. setColor (solidColor );
SetBackground (gradientDrawable );
}
}
Return super. onTouchEvent (event );
}


Private GradientDrawable. Orientation getOrientation (int gradientOrientation ){
GradientDrawable. Orientation orientation = null;
Switch (gradientOrientation ){
Case 0:
Orientation = GradientDrawable. Orientation. BL_TR;
Break;
Case 1:
Orientation = GradientDrawable. Orientation. BOTTOM_TOP;
Break;
Case 2:
Orientation = GradientDrawable. Orientation. BR_TL;
Break;
Case 3:
Orientation = GradientDrawable. Orientation. LEFT_RIGHT;
Break;
Case 4:
Orientation = GradientDrawable. Orientation. RIGHT_LEFT;
Break;
Case 5:
Orientation = GradientDrawable. Orientation. TL_BR;
Break;
Case 6:
Orientation = GradientDrawable. Orientation. TOP_BOTTOM;
Break;
Case 7:
Orientation = GradientDrawable. Orientation. TR_BL;
Break;
}
Return orientation;
}

}

3. xml add this xmlns: flatui = "http://schemas.android.com/apk/res-auto" (not explained)

<! --
Flatui: strokeDashGap = "5dp"
Flatui: strokeDashWidth = "5dp"
Flatui: gradientOrientation = "BOTTOM_TOP"
-->

<Com. klower. component. ShapeTextView
Android: id = "@ + id/shapetextview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: gravity = "center"
Android: padding = "5dp"
Android: text = "ShapeTextview"
Android: textSize = "25sp"
Flatui: bottomLeftRadius = "10dp"
Flatui: bottomRightRadius = "0dp"
Flatui: gradientCenterColor = "#00000000"
Flatui: gradientEndColor = "# 64DE0E"
Flatui: gradientStartColor = "# D11B1E"
Flatui: solidColor = "# 64D11C"
Flatui: stroke_Color = "# D11B1E"
Flatui: stroke_Width = "2dp"
Flatui: topLeftRadius = "0dp"
Flatui: topRightRadius = "10dp"
Flatui: touchSolidColor = "# F5B2B9"/>

<Com. klower. component. ShapeTextView
Flatui: strokeDashGap = "5dp"
Flatui: strokeDashWidth = "5dp"
Android: id = "@ + id/shapetextview1"
Android: layout_width = "100dp"
Android: layout_height = "100dp"
Android: layout_margin = "10dp"
Android: gravity = "center"
Android: padding = "5dp"
Android: text = "ORAL"
Android: textSize = "25sp"
Flatui: gradientCenterColor = "#00000000"
Flatui: gradientEndColor = "# 64DE0E"
Flatui: gradientStartColor = "# D11B1E"
Flatui: solidColor = "# 64D11C"
Flatui: stroke_Color = "# D11B1E"
Flatui: stroke_Width = "2dp"
Flatui: shapeType = "OVAL"
Flatui: touchSolidColor = "# F5B2B9"/>

You can view the source code and debug the attributes as long as you want to make the results.

Leave a message if you have any questions.

 

 

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.