我是靠谱客的博主 俊秀薯片,最近开发中收集的这篇文章主要介绍Android--高级TextViewCompound DrawableShadowCustom fontGradientPatternHTMLStacked fractions,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Compound Drawable
给TextView
设置Drawable
,代替使用ImageView
+TextView
的情况。
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/animation"
android:drawableLeft="@drawable/rotating_loading"
android:drawableRight="@drawable/animated_wifi"
android:drawableBottom="@drawable/animated_clock"
android:drawablePadding="8dp"
tools:context=".AnimatedCompoundDrawableActivity"/>
AnimatedRotateDrawable
同时也可以给TextView
中的Drawable
设置动画。
rotating_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/ic_loading"
android:duration="500" />
AnimationDrawable
animated_wifi.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_wifi_0" android:duration="250" />
<item android:drawable="@drawable/ic_wifi_1" android:duration="250" />
<item android:drawable="@drawable/ic_wifi_2" android:duration="250" />
<item android:drawable="@drawable/ic_wifi_3" android:duration="250" />
</animation-list>
AnimatedVectorDrawable
animated_clock.xml
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/clock" >
<target
android:name="hours"
android:animation="@anim/hours_rotation" />
<target
android:name="minutes"
android:animation="@anim/minutes_rotation" />
</animated-vector>
通过代码执行动画
private void startAnimation(
TextView textView) {
Drawable[] drawables
= textView.getCompoundDrawables();
for (Drawable drawable : drawables) {
if (drawable != null &&
drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
}
Shadow
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="@string/shadow"
android:textSize="80sp"
android:textStyle="bold"
android:shadowColor="#7000"
android:shadowDx="12"
android:shadowDy="12"
android:shadowRadius="8"/>
其中有关于Shadow
的属性有shadowColor, shadowDx, shadowDy, shadowRadius
,注意shadowDx, shadowDy, shadowRadius
的单位是px
不是dp
Custom font
Typeface typeface= Typeface.createFromAsset(
getAssets(), "Ruthie.ttf");
textView.setTypeface(typeface);
`
Gradient
Shader shader = new LinearGradient(
0, 0, 0, textView.getTextSize(),
Color.RED, Color.BLUE,
Shader.TileMode.CLAMP);
textView.getPaint().setShader(shader);
Pattern
Bitmap bitmap
= BitmapFactory.decodeResource(
getResources(),
R.drawable.cheetah_tile);
Shader shader = new BitmapShader(
bitmap,
Shader.TileMode.REPEAT,
Shader.TileMode.REPEAT);
textView.getPaint().setShader(shader);
HTML
原始的Html
<h1>Hello World</h1>
Here is an
<img src="octopus"><i>octopus</i>.<br>
And here is a
<a href="http://d.android.com">
link</a>
在xml
中
<string name="from_html_text">
<![CDATA[
<h1>Hello World</h1>
Here is an
<img src="octopus"><i>octopus</i>.<br>
And here is a
<a href="http://d.android.com">
link</a>.
]]>
</string>
setMovementMethod()
public class FromHtmlActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_from_html);
TextView textView = (TextView) findViewById(R.id.text);
String html = getString(R.string.from_html_text);
textView.setText(Html.fromHtml(html, new ResouroceImageGetter(this), null));
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
private static class ResouroceImageGetter implements Html.ImageGetter {
private final Context context;
public ResouroceImageGetter(Context context) {
this.context = context;
}
@Override
public Drawable getDrawable(String source) {
int path = context.getResources().getIdentifier(source, "drawable",
BuildConfig.APPLICATION_ID);
Drawable drawable = context.getResources().getDrawable(path);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}
}
}
Stacked fractions
自定义Tag
<string name="fraction_text">
<![CDATA[
1<afrc>1/2</afrc> + <afrc>11/16</afrc>
= 2<afrc>3/16</afrc>
]]>
</string>
自定义Tag Handle
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class FractionActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fraction);
TextView textView = (TextView) findViewById(R.id.text);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Nutso2.otf");
textView.setTypeface(typeface);
String html = getString(R.string.fraction_text);
textView.setText(Html.fromHtml(html, null, new FractionTagHandler()));
}
// http://stackoverflow.com/questions/4044509/android-how-to-use-the-html-taghandler
private static class FractionTagHandler implements Html.TagHandler {
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if (!"afrc".equalsIgnoreCase(tag)) {
return;
}
int len = output.length();
if (opening) {
output.setSpan(new FractionSpan(), len, len, Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, FractionSpan.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
if (where != len) {
output.setSpan(new FractionSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
private Object getLast(Editable text, Class kind) {
Object[] objs = text.getSpans(0, text.length(), kind);
if (objs.length == 0) {
return null;
} else {
for (int i = objs.length - 1; i >= 0; --i) {
if(text.getSpanFlags(objs[i]) == Spannable.SPAN_MARK_MARK) {
return objs[i];
}
}
return null;
}
}
}
private static class FractionSpan extends MetricAffectingSpan {
private static final String FONT_FEATURE_SETTINGS = "afrc";
@Override
public void updateMeasureState(TextPaint textPaint) {
textPaint.setFontFeatureSettings(FONT_FEATURE_SETTINGS);
}
@Override
public void updateDrawState(TextPaint textPaint) {
textPaint.setFontFeatureSettings(FONT_FEATURE_SETTINGS);
}
}
}
最后
以上就是俊秀薯片为你收集整理的Android--高级TextViewCompound DrawableShadowCustom fontGradientPatternHTMLStacked fractions的全部内容,希望文章能够帮你解决Android--高级TextViewCompound DrawableShadowCustom fontGradientPatternHTMLStacked fractions所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复