1:html
android自定義控件時,一般須要重寫父類構造函數。這三個夠找函數具體啥時調用?java
public View (Context context) 是在java代碼建立視圖的時候被調用,若是是從xml填充的視圖,就不會調用這個
public View (Context context, AttributeSet attrs) 這個是在xml建立可是沒有指定style的時候被調用
public View (Context context, AttributeSet attrs, int defStyle) 在xml建立並指定style的時候被調用android
若是在Code中實例化一個View會調用第一個構造函數,若是在xml中定義會調用第二個構造函數,而第三個函數系統是不調用的,要由View(咱們自定義的或系統預約義的View,如此處的CustomTextView和Button)顯式調用,好比在這裏咱們在第二個構造函數中調用了第三個構造函數,並將R.attr.CustomizeStyle傳給了第三個參數。canvas
關於第三個函數,這裏附帶一篇長博:http://www.cnblogs.com/angeldevil/p/3479431.htmlide
2:API 11 以後不能使用硬件渲染,因此須要關閉硬件加速,view級別的關閉硬件加速比較好,this.setLayerType(View.LAYER_TYPE_SOFTWARE,null);加到自定義的View 類中的構造函數就能夠函數
3:測試
//爲SeekBar添加滑動事件
mSkBarGifPlay.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});this
出錯的話,多是沒有引入相應的包。.net
4.Activity的的方法中使用Toast。code
Toast.makeText(getBaseContext(), Integer.toString(progress), Toast.LENGTH_SHORT).show();
或者Toast.makeText(XXXActivity.this, Integer.toString(progress), Toast.LENGTH_SHORT).show();
所顯示的內容必須是字符串,不然會crash。
5.gif圖片能夠經過拉伸Canvas來作到
核心代碼 本身加個layout,Activity測試
public class GifView extends View {
private Movie mMovie;
private long mMovieStart;
private int mWidth, mHeight;
private int mViewWidht, mViewHeight;
private OnPlayListener onPlayListener;
public GifView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public GifView(Context context) {
super(context);
mMovie = Movie.decodeStream(getResources().openRawResource(
R.raw.gif_anim));
}
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
//使用Movie解析gif圖片
mMovie = Movie.decodeStream(getResources().openRawResource( R.raw.gif_anim));
//得到屏幕寬度,高度
mWidth = BaseApplication.getInstance().screenWidth;
mHeight = BaseApplication.getInstance().screenHeight;
//gif圖片寬度,高度
mViewHeight = mMovie.height();
mViewWidht = mMovie.width();
}
public OnPlayListener getOnPlayListener() {
return onPlayListener;
}
public void setOnPlayListener(OnPlayListener onPlayListener) {
this.onPlayListener = onPlayListener;
}
boolean isDraw = true;
public void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
if (isDraw) {
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 5000;
}
//計算gif播放時間,gif播放完成,關閉界面
if (now - mMovieStart >= dur) {
isDraw = false;
if (onPlayListener != null) {
onPlayListener.onFinished();
}
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
//根據屏幕大小計算縮放比例
float saclex = (float) mWidth / (float) mViewWidht;
float sacley = (float) mHeight / (float) mViewHeight;
float sameRate = saclex > sacley ? saclex : sacley;
canvas.scale(sameRate, sameRate);
mMovie.draw(canvas, 0, 0);
invalidate();
}
}
}
//gif關閉接口
public static interface OnPlayListener {
public void onFinished();
}
}
參考:http://www.apkbus.com/android-142921-1-1.html
6.自定義View中No resource identifier found for attribute X in package X。
解決方法路徑中只須要寫本身的AndroidManifest文件中的包名便可。
如:xmlns:attr="http://schemas.android.com/apk/res/com.vane.demo"
參考:http://blog.csdn.net/xiaoguohaha/article/details/12676691
7.Thread.sleep須要捕捉異常操做
try { Thread.sleep(800); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }