Titanic is a simple illusion obtained by applying an animated translation on the TextView TextPaint Shader‘s matrix.
Titanic的使用
Titanic的使用,项目结构如下:
一、下载Titanic并且部署到项目中,
在项目中我们使用Titanic需要三个文件: Titanic.java、TitanicTextView.java和wave.png。当然wave.png是可以修改的,在TitanicTextView.java中引用了该资源。
二、它的使用比较简单,就是和普通的自定义View的使用一样。
在xml中定义TitanicTextView
开始动画
titanic = newTitanic();
titanic.start(myTitanicTextView);
取消动画
titanic.cancel();
三、我们还可以修改它的字体,这也和普通的修改字体一样。
在assets中引入ttf字体,在这里我们可以写一个有的帮助类Typefaces
packagecom.example.titanictextview;importandroid.content.Context;importandroid.graphics.Typeface;importandroid.util.Log;importjava.util.Hashtable;/*** Created by Linux on 2016/6/9.*/
public classTypefaces {private static final String TAG = "Typefaces";private static final Hashtable cache = new Hashtable();public staticTypeface 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;
}
}returncache.get(assetPath);
}
}
}
TitanicTextView的setText方法前,调用
titanicTextView.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf"));
四、运行效果如下:
MainActivity.java:
packagecom.example.titanictextview;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.view.View;importcom.romainpiel.titanic.library.Titanic;importcom.romainpiel.titanic.library.TitanicTextView;public class MainActivity extendsAppCompatActivity {privateTitanicTextView titanicTextView;privateTitanic titanic;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titanicTextView=(TitanicTextView) findViewById(R.id.titanic_tv);
titanic= newTitanic();
}//点击开始
public voidstartTitianic(View view) {
titanicTextView.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf"));
titanicTextView.setText("I love you!");
titanic.start(titanicTextView);
}//点击结束
public voidcancelTitianic(View view) {
titanic.cancel();
}
}
View Code
友情链接
原文:http://www.cnblogs.com/huhx/p/androidTitanicView.html