のカスタムクラスがありTextView
ます。テキストビューの色としてグラデーション属性を実装しました。しかし、私はそれをで実装することができましたxml
。カスタムビューは初めてです。私は、私が追加できるかわからないsetStartColor
、setEndColor
私のカスタムのTextViewクラスで。
値/属性
<declare-styleable name="GradientTextView">
<attr name="startColor" format="color" />
<attr name="endColor" format="color" />
</declare-styleable>
GradientTextView
public class GradientTextView extends AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GradientTextView);
int startColor = a.getColor(R.styleable.GradientTextView_startColor, Color.WHITE);
int endColor = a.getColor(R.styleable.GradientTextView_endColor, Color.WHITE);
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
a.recycle();
}
}
XML
<mehran.design.GradientTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:startColor="@color/yellow"
app:endColor="@color/blue"/>
変数startColor
とendColor
そのセッターを定義します
お気に入り
public void setStartColor(int color) {
this.startColor= color;
--- do your logic----
invalidate();
}
参照リンク
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加