ProgressBar自定义图片拉伸的问题
2016-05-14 19:59
288 查看
今天在公司写一个下拉刷新的控件时,遇到的问题,在这里记录一下,
当时的想要的效果是这样的
刷新控件里面的跳动图片刚好是图片的宽度,但是在写好测试过的过程中,测试时的结果是这样的,他会把一行都用图片沾满,因为我当时的布局是这样子的,
和
如果这样子写的话,我原来的图片
就被拉伸了
,拉伸的结果就是在宽度上沾满了整个屏幕,
后来百思也不知道原因,看了源码之后,才有点感悟,我们来看下源码,
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mUiThreadId = Thread.currentThread().getId();
initProgressBr( );
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
mNoInvalidate = true;
final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
if (progressDrawable != null) {
}
}这里主要是获取我们要加载的
图片,获取到之后,
if (progressDrawable != null) {
// Calling this method can set mMaxHeight, make sure the corresponding
setProgressDrawableTiled(progressDrawable);
(d)
;
}
public void setProgressDrawableTiled(Drawable d) {
if (d != null) {
d = tileify(d, false);
}
setProgressDrawable(d);
}
这个tileify方法里
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable
;
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
} else if (drawable instanceof StateListDrawable) {
StateListDrawable in = (StateListDrawable) drawable;
StateListDrawable out = new StateListDrawable();
int numStates = in.getStateCount();
for (int i = 0; i < numStates; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
} else if (drawable instanceof BitmapDrawable) {
final BitmapDrawable bitmap = (BitmapDrawable) drawable;
final Bitmap tileBitmap = bitmap.getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader); // Ensure the tint and filter are propagated in the correct order. shapeDrawable.setTintList(bitmap.getTint()); shapeDrawable.setTintMode(bitmap.getTintMode()); shapeDrawable.setColorFilter(bitmap.getColorFilter()); return clip ? new ClipDrawable( shapeDrawable, Gravity.LEFtaT, return drawable; }
}
他在这个方法里final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); ble.HORIZONTAL) : shapeDrawable; }
} final BitmapShader bitmapShader = new BitmapShader(e.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader);
所以他就有了重复显示,以至于沾满了整个宽度,后来我把我的成了这样。
,就没有把整个屏幕沾满的结果了,图片多大,我们的显示就只有多大,就没有把整个屏幕沾满的结果了,图片多大,我们的显示就只有多大,记一下这个, style=”?android:attr/progressBar.Style.Inverse”,这个指向的是attr文件里的
,这个reference又指向的是themes.xml 里面的@style/Widget.ProgressBar.Small.Inverse,Widget.ProgressBar指向的就是style.xml里面具体的style了。这里指向的是
.
当时的想要的效果是这样的
刷新控件里面的跳动图片刚好是图片的宽度,但是在写好测试过的过程中,测试时的结果是这样的,他会把一行都用图片沾满,因为我当时的布局是这样子的,
和
如果这样子写的话,我原来的图片
就被拉伸了
,拉伸的结果就是在宽度上沾满了整个屏幕,
后来百思也不知道原因,看了源码之后,才有点感悟,我们来看下源码,
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mUiThreadId = Thread.currentThread().getId();
initProgressBr( );
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
mNoInvalidate = true;
final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
if (progressDrawable != null) {
}
}这里主要是获取我们要加载的
图片,获取到之后,
if (progressDrawable != null) {
// Calling this method can set mMaxHeight, make sure the corresponding
setProgressDrawableTiled(progressDrawable);
}我们来看下setProgressDrled这个方法中
(d)
;
}
public void setProgressDrawableTiled(Drawable d) {
if (d != null) {
d = tileify(d, false);
}
setProgressDrawable(d);
}
这个tileify方法里
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable
;
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
} else if (drawable instanceof StateListDrawable) {
StateListDrawable in = (StateListDrawable) drawable;
StateListDrawable out = new StateListDrawable();
int numStates = in.getStateCount();
for (int i = 0; i < numStates; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
} else if (drawable instanceof BitmapDrawable) {
final BitmapDrawable bitmap = (BitmapDrawable) drawable;
final Bitmap tileBitmap = bitmap.getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader); // Ensure the tint and filter are propagated in the correct order. shapeDrawable.setTintList(bitmap.getTint()); shapeDrawable.setTintMode(bitmap.getTintMode()); shapeDrawable.setColorFilter(bitmap.getColorFilter()); return clip ? new ClipDrawable( shapeDrawable, Gravity.LEFtaT, return drawable; }
}
他在这个方法里final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); ble.HORIZONTAL) : shapeDrawable; }
} final BitmapShader bitmapShader = new BitmapShader(e.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader);
所以他就有了重复显示,以至于沾满了整个宽度,后来我把我的成了这样。
,就没有把整个屏幕沾满的结果了,图片多大,我们的显示就只有多大,就没有把整个屏幕沾满的结果了,图片多大,我们的显示就只有多大,记一下这个, style=”?android:attr/progressBar.Style.Inverse”,这个指向的是attr文件里的
,这个reference又指向的是themes.xml 里面的@style/Widget.ProgressBar.Small.Inverse,Widget.ProgressBar指向的就是style.xml里面具体的style了。这里指向的是
.
相关文章推荐
- 1008 of dp
- HashMap实现原理
- Sass的知识点梳理
- 第十一周项目49-实现类中的运算符重载
- 第十一周学习进度条
- 获取Apple设备语言的方式
- 【斜率优化】BZOJ1010 [HNOI2008]玩具装箱toy
- 使用hexo搭建博客并上传GitHub
- 在windows7中配置cocos2dx开发环境
- XML_Java_ XML_利用Jdom(Java)解析xml文件 示例
- QT——快捷键
- 第十一周进度条
- 斐波那契数列(Fibonacci sequence)的前200项
- Codeforces Round #246 (Div. 2) C. Prime Swaps
- MySQL SQL优化
- spring hibernate 批量插入jdbc及hibernate方法
- 关于编码转换
- vim基础学习
- Matlab读取excel数据,并绘图生成exe可执行文件
- Problem3-1011