您的位置:首页 > 其它

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

}我们来看下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了。这里指向的是

.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: