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(); }