android架構---->下沉文字Titanic的使用

來源:互聯網
上載者:User

標籤:

  Titanic is a simple illusion obtained by applying an animated translation on the TextView TextPaint Shader‘s matrix.

 

Titanic的使用

Titanic的使用,項目結構如下:


一、下載Titanic並且部署到項目中,

Titanic的項目地址: https://github.com/RomainPiel/Titanic。

在項目中我們使用Titanic需要三個檔案: Titanic.java、TitanicTextView.java和wave.png。當然wave.png是可以修改的,在TitanicTextView.java中引用了該資源。

 

二、它的使用比較簡單,就是和普通的自訂View的使用一樣。

在xml中定義TitanicTextView

<com.romainpiel.titanic.library.TitanicTextView    android:id="@+id/titanic_tv"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textColor="#212121"    android:textSize="70sp"

開始動畫

titanic = new Titanic();titanic.start(myTitanicTextView);

取消動畫

titanic.cancel();

 

三、我們還可以修改它的字型,這也和普通的修改字型一樣。

在assets中引入ttf字型,在這裡我們可以寫一個有的協助類Typefaces

package com.example.titanictextview;import android.content.Context;import android.graphics.Typeface;import android.util.Log;import java.util.Hashtable;/** * Created by Linux on 2016/6/9. */public class Typefaces {    private static final String TAG = "Typefaces";    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();    public static Typeface get(Context c, String assetPath) {        synchronized (cache) {            if (!cache.containsKey(assetPath)) {                try {                    Typeface t = Typeface.createFromAsset(c.getAssets(), assetPath);                    cache.put(assetPath, t);                } catch (Exception e) {                    Log.e(TAG, "Could not get typeface ‘" + assetPath + "‘ because " + e.getMessage());                    return null;                }            }            return cache.get(assetPath);        }    }}

TitanicTextView的setText方法前,調用

titanicTextView.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf"));

 

四、運行效果如下:

MainActivity.java:

package com.example.titanictextview;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.romainpiel.titanic.library.Titanic;import com.romainpiel.titanic.library.TitanicTextView;public class MainActivity extends AppCompatActivity {    private TitanicTextView titanicTextView;    private Titanic titanic;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        titanicTextView = (TitanicTextView) findViewById(R.id.titanic_tv);        titanic = new Titanic();    }    // 點擊開始    public void startTitianic(View view) {        titanicTextView.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf"));        titanicTextView.setText("I love you!");        titanic.start(titanicTextView);    }    // 點擊結束    public void cancelTitianic(View view) {        titanic.cancel();    }}
View Code

 

友情連結

 

android架構---->下沉文字Titanic的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.