美文网首页
Android炫酷应用300例读书笔记三

Android炫酷应用300例读书笔记三

作者: 梧叶已秋声 | 来源:发表于2019-09-29 09:45 被阅读0次

第3章文字
65.使用ScaleXSpan创建扁平风格的文字

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("实例锦集");
        spannableString.setSpan(new ScaleXSpan(3),0,spannableString.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
    }
    
}

66.使用MaskFilterSpan实现文字边缘模糊

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="150sp"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("炫酷");
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(21,BlurMaskFilter.Blur.SOLID));
        spannableStringBuilder.setSpan(maskFilterSpan,0,2,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        mTextView.setText(spannableStringBuilder);
    }

}

67.使用MaskFilterSpan实现文字中心镂空

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("炫酷");
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(1,BlurMaskFilter.Blur.OUTER));
        spannableStringBuilder.setSpan(maskFilterSpan,0,2,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        mTextView.setText(spannableStringBuilder);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="150sp"/>
</LinearLayout>

68.使用MaskFilterSpan实现文字整体模糊

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("炫酷");
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(15,BlurMaskFilter.Blur.NORMAL));
        spannableStringBuilder.setSpan(maskFilterSpan,0,2,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        mTextView.setText(spannableStringBuilder);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="150sp"/>
</LinearLayout>

69.使用MaskFilterSpan模糊多个字符串

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="关键词:"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="169dp"
        android:layout_height="51dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toLeftOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:text="微软 [1]  (英文名称:microsoft,中文简称:微软公司或美国微软公司)始建于1975年,是一家美国跨国科技公司,也是世界PC(Personal Computer,个人计算机)软件开发的先导,由比尔·盖茨与保罗·艾伦创办于1975年,公司总部设立在华盛顿州的雷德蒙德(Redmond,邻近西雅图)。以研发、制造、授权和提供广泛的电脑软件服务业务为主。"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="40dp"
        />
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
    private EditText mEditText;
    private TextView mTextView;
    private Button mButton;
    private String mString;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().getDecorView().setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        mTextView = (TextView) findViewById(R.id.textView2);
        mString = mTextView.getText().toString();
        mEditText = (EditText) findViewById(R.id.edit_text);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String search  =mEditText.getText().toString();
                SpannableString spannableString = new SpannableString(mString);
                for (int i = 0 ; i< mString.length();i++){
                    int start = i;
                    i = mString.indexOf(search,i);
                    Log.d("MAIN","i = " + i);
                    if (i<0){
                        spannableString.setSpan(new MaskFilterSpan(new BlurMaskFilter(8,BlurMaskFilter.Blur.NORMAL)),start+search.length()-1,mString.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        break;
                    }
                    spannableString.setSpan(new MaskFilterSpan(new BlurMaskFilter(8,BlurMaskFilter.Blur.NORMAL)),start+search.length()-1,i,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                mTextView.setText(spannableString);
            }
        });
    }

}

输入文字后,点击button,除了输入的文字之外的文字就会被模糊掉。

70.使用BulletSpan在文本首字前添加小圆点

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("实例锦集");
        spannableString.setSpan(new BulletSpan(50,Color.RED),0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
    }

}

71.使用StrikethroughSpan添加文字删除线

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("云计算是分布式计算的一种,指的是通过网络“云”将巨大的数据计算处理程序分解成无数个小程序。");
        spannableString.setSpan(new StrikethroughSpan(),26,29,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

72.使用URLSpan为部分内容添加超链接

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="30sp"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.textView);
        SpannableString spannableString = new SpannableString("随便找一个,电话,百度,地址");
        int startPos = spannableString.toString().indexOf("电话");
        int endPos = startPos + 2;
        spannableString.setSpan(new URLSpan("tel:12345678901"),startPos,endPos,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        startPos = spannableString.toString().indexOf("百度");
        endPos = startPos + 2;
        spannableString.setSpan(new URLSpan("https://www.baidu.com"),startPos,endPos,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        startPos = spannableString.toString().indexOf("地址");
        endPos = startPos + 2;
        spannableString.setSpan(new URLSpan("geo:38.899533,-77.036476"),startPos,endPos,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTextView.setText(spannableString);
        mTextView.setMovementMethod(LinkMovementMethod.getInstance());
    }

}

参考链接:
TextView中添加超链接
https://www.jianshu.com/p/29a379512a13

73.使用ImageSpan同时显示QQ表情和文字


qq.png
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("大爷,伤不起啊!我伤不起啊!伤不起啊!");
        int startPos = spannableString.toString().indexOf("我伤不起啊!");
        int endPos = startPos + "我伤不起啊!".toString().length();
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.qq, null);
        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
        spannableString.setSpan(new ImageSpan(drawable),startPos,endPos,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

74.使用StyleSpan实现以粗斜体显示文字

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("云计算是分布式计算的一种,指的是通过网络“云”将巨大的数据计算处理程序分解成无数个小程序。");
        spannableString.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),26,29,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

75.使用SuperscriptSpan绘制勾股定理公式

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("勾股定理公式:a2+b2=c2");
        spannableString.setSpan(new SuperscriptSpan(),8,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new SuperscriptSpan(),11,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new SuperscriptSpan(),14,15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(0.5f),8,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(0.5f),11,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(0.5f),14,15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTextView.setText(spannableString);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

76.使用SubscriptSpan绘制硫酸亚铁分子式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableString spannableString = new SpannableString("硫酸亚铁的分子式:FeSO4");
        int fesPos = spannableString.toString().indexOf("4");
        spannableString.setSpan(new SubscriptSpan(),fesPos,fesPos+1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(0.8f),fesPos,fesPos+1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTextView.setText(spannableString);
    }

}

77.使用TypefaceSpan定制文本的部分内容
首先,下载字体。
https://github.com/microsoft/cascadia-code/releases

image.png
放到assets目录下。
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        mTextView.setText("abab");
        Typeface typeface = Typeface.createFromAsset(getAssets(),"Cascadia.ttf");
       mTextView.setTypeface(typeface);
        SpannableString spannableString = new SpannableString(mTextView.getText());
        spannableString.setSpan(new TypefaceSpan("Cascadia"),2,mTextView.getText().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);

    }

}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

78.使用ForegroundColorSpan创建光照文字
首先,manifest中加入,下面的代码,否则字会不显示。

 android:hardwareAccelerated="false"
image.png
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        mTextView.setText("google谷歌");
        SpannableString spannableString = new SpannableString(mTextView.getText());
        //应用红色前景
        spannableString.setSpan(new ForegroundColorSpan(Color.RED),0,6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
       spannableString.setSpan(new MaskFilterSpan(new EmbossMaskFilter(
               new float[]{1,1,1},0.25f,6,3.5f
       )),0,6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//应用光照特效
        mTextView.setText(spannableString);

    }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

最后运行结果依然是魅族机型不显示,小米可以显示。这种特效太受限于机型了,实用性不强。
79.使用BlurMaskFilter创建阴影扩散文字

public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(new MyView(this));
        super.onCreate(savedInstanceState);
    }

    class MyView extends View{
        private Paint mPaint;
        public MyView(Context context) {
            super(context);
            mPaint = new Paint();
            mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.BLACK);
            mPaint.setTextSize(200);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setStrokeWidth(2);
            BlurMaskFilter blurMaskFilter = new BlurMaskFilter(40,BlurMaskFilter.Blur.SOLID);
            mPaint.setMaskFilter(blurMaskFilter);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            canvas.drawText("炫酷应用",width/10,height/2-150,mPaint);
        }
    }
}

80.使用EmbossMaskFilter创建浮雕文字
首先,manifest中加入,下面的代码,否则字会不显示。

 android:hardwareAccelerated="false"
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("炫酷");
        float[] direction = new float[]{10,10,10};//设置光源方向
        float light = 0.1f;//环境光亮度
        float specular = 5;//反射等级
        float blur = 5;//模糊半径
        EmbossMaskFilter embossMaskFilter = new EmbossMaskFilter(direction,light,specular,blur);
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(embossMaskFilter);
        spannableStringBuilder.setSpan(maskFilterSpan,0,2,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        mTextView.setText(spannableStringBuilder);
    }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>

81.通过自定义View在半圆弧上绘制文字

public class CustomTextView extends View{
    private String mTextString = "android 炫酷实例集锦";
    private Paint mPaint;
    private Path mPath;

    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.translate(100,490);
        canvas.drawTextOnPath(mTextString,mPath,3,-10,mPaint);
    }

    public void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(96);
        LinearGradient linearGradient = new LinearGradient(100,100,800,800,
                Color.GREEN,Color.BLUE, Shader.TileMode.MIRROR);
        mPaint.setShader(linearGradient);
        if (mPath == null){
            mPath = new Path();
            mPath.addArc(100,100,800,800,180,180);
        }
    }
    
}
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.cc.uisample.CustomTextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="30sp"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

}

82.通过自定义View在圆弧上滚动文字

public class CustomTextView extends View{
    private String mTextString = "人生得意须尽欢";
    private Paint mPaint;
    private Path mPath;
    private Timer mTimer;
    private TimerTask mTimerTask;
    private float mPosX = 0;

    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.translate(150,300);
        canvas.drawTextOnPath(mTextString,mPath,mPosX,-10,mPaint);
    }

    public void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(80);
        if (mPath == null){
            mPath = new Path();
            mPath.addArc(10,10,700,700,0,360);
        }
    }

    public void starTask(){
        if (mTimerTask != null){
            mTimerTask.cancel();
        }
        mTimer = new Timer();
        mTimerTask = new CustomTimerTask();
        mTimer.schedule(mTimerTask,0,100);
    }
    public void stopTask(){
        mTimerTask.cancel();
    }

    private class CustomTimerTask extends TimerTask{

        @Override
        public void run() {
            mPosX += 10;
            if (mPosX == 1500){
                mPosX = 0;
            }
            postInvalidate();
        }
    }
}
public class MainActivity extends AppCompatActivity {
    private CustomTextView mCustomTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCustomTextView = (CustomTextView) findViewById(R.id.textView);
    }

    public void start(View view){
        mCustomTextView.starTask();
    }
    public void stop(View view){
        mCustomTextView.stopTask();
    }
}
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.cc.uisample.CustomTextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="85dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="30sp"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button2"
        android:layout_marginRight="8dp"
        android:onClick="start"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"
        android:layout_marginRight="55dp"
        app:layout_constraintRight_toRightOf="parent"
        android:onClick="stop"/>


</android.support.constraint.ConstraintLayout>

83.通过自定义View绘制渐变色的文字

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(new MyView(this));
        super.onCreate(savedInstanceState);

    }

    class MyView extends View{
        private Paint mPaint;
        public MyView(Context context) {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            Shader shader = new LinearGradient(0,0,160,160,
                    new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},null, Shader.TileMode.REPEAT);
            mPaint = new Paint();
            mPaint.setShader(shader);
            mPaint.setTextSize(160);
            canvas.drawColor(Color.WHITE);
            canvas.drawText("炫酷应用",50,height/2-200,mPaint);
        }
    }
}

84.通过自定义View绘制线条描边文字

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(new MyView(this));
        super.onCreate(savedInstanceState);

    }

    class MyView extends View{
        private Paint mPaint;
        public MyView(Context context) {
            super(context);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.BLUE);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setTextSize(220);
            mPaint.setStrokeWidth(1);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            canvas.scale(1.0f,1.5f);
            canvas.drawText("炫酷应用",width/10,height * 3/10,mPaint);
        }
    }
}

85.通过自定义View绘制阴影扩散文字

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(new MyView(this));
        super.onCreate(savedInstanceState);

    }

    class MyView extends View{
        private Paint mPaint;
        public MyView(Context context) {
            super(context);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.BLUE);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setTextSize(200);
            mPaint.setStrokeWidth(2);
            mPaint.setShadowLayer(15f,10f,10f,Color.GRAY);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            canvas.scale(1.0f,1.5f);
            canvas.drawText("炫酷应用",width/10,height * 2/5,mPaint);
        }
    }
}

86.加载字库文件显示自定义草书字体
字体是要钱的,所以em。。还是用微软的吧。
下载地址77.使用TypefaceSpan定制文本的部分内容
有。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:text="asdjhuigiyf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        Typeface typeface = Typeface.createFromAsset(getAssets(),"Cascadia.ttf");
        mTextView.setTypeface(typeface);
    }
    
}

87.加载字库文件显示自定义液晶字体
字体是要钱的,所以依旧还是用微软的吧。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">

    <TextView
        android:id="@+id/text_view"
        android:text="asdjhuigiyf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        AssetManager assetManager = getAssets();
        Typeface typeface = Typeface.createFromAsset(assetManager,"Cascadia.ttf");
        mTextView.setTypeface(typeface);
    }


}

88.判断在一个字符串中是否包含汉字

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:text="测试内容:"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintLeft_toRightOf="@+id/text_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="64dp"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
    private EditText mEditText;
    private Button mButton;
    private String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText) findViewById(R.id.edit_text);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String string = mEditText.getText().toString();
                Pattern pattern = Pattern.compile(format);
                Matcher matcher = pattern.matcher(string);
                boolean result = matcher.find();
                if (result){
                    Toast.makeText(MainActivity.this,"有汉字",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this,"无汉字",Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

}

相关文章

网友评论

      本文标题:Android炫酷应用300例读书笔记三

      本文链接:https://www.haomeiwen.com/subject/daovuctx.html