[英]SeekBar: getting empty bitmap from Drawable until set it to ImageView OR how to get resulting Bitmap from 9patch
So I want to set a Bitmap created from drawable into a SeekBar's progress. 因此,我想将从可绘制对象创建的位图设置为SeekBar的进度。 And I make it so: 我做到了:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.seekbar_bg_full);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // I assume here drawable must be drawn but its not
// canvas.drawBitmap(bmp, 0 , 0, null); // does nothing as 4 me
// encode/decode to detach bitmap from 9patch
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0, baos);
final byte[] bytes = baos.toByteArray();
bmp.recycle();
bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
// ClipDrawable is intented to be used as progressDrawable in SeekBar
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
// if not set this drawable to an ImageView then no progress will be shown by SeekBar at all
//ImageView imgFake = (ImageView) findViewById(R.id.fakeImageView);
//imgFake.setImageDrawable(progressDrawable);
mySeekBar.setProgressDrawable(progressDrawable);
width
and height
are valid values here (like 460 and 30). width
和height
是此处的有效值(例如460和30)。 As you can see there are 2 code lines about ImageView
are commented. 如您所见,有2条关于ImageView
代码行已注释。 This ImageView
persists on a layout and its visibility is INVISIBLE. 该ImageView
保留在布局上,其可见性不可见。 If I comment those 2 lines like shown then there will be no visible progress, like drawable is empty or transparent. 如果我对显示的这两行进行注释,那么将没有可见的进度,例如drawable为空或透明。 Looks like this ImageView
makes drawable to really draw itself. 看起来像这样的ImageView
使得drawable能够真正绘制自己。 But I don't like to use a fake ImageView
just to make the "magic" happen so the question is - how to make it work without this fake ImageView
. 但是我不喜欢使用伪造的ImageView
只是为了使“魔术”发生,所以问题是-如何在没有这个伪造的ImageView
情况下使其工作。
Please don't suggest me ways how to properly set SeekBar
progress like: 请不要建议我如何正确设置SeekBar
进度的方法,例如:
ClipDrawable progressDrawable = new ClipDrawable(getResources().getDrawable(R.drawable.seekbar_bg_full), Gravity.LEFT, ClipDrawable.HORIZONTAL);
mySeekBar.setProgressDrawable(progressDrawable);
or xml selectors ways or any altrnative ways since I know about it already and my quiestion is not really about it. 或xml选择器方式或任何其他方式,因为我已经知道了,而我的问题并不是真的。 I just need to make it work my way. 我只需要使它按我的方式工作即可。
I just need to make my bitmap or canvas or whatever really drawn. 我只需要制作我的位图或画布或任何真正绘制的东西。
A bit more details if you want (optional to read). 如果需要,可以提供更多细节(可选阅读)。 The problem is about the drawable seekbar_bg_full
- its a 9-patch png. 问题在于可绘制的seekbar_bg_full
-9补丁png。 And all need is to obtain a not NinePatchDrawable-linked resulting Bitmap. 而所有需要的是获得未与NinePatchDrawable链接的结果位图。 Assume I have a 460x30px view with 9patch image set as src or background and the 9patch image is stretched just like it should to. 假设我有一个460x30px的视图,其中9patch图像设置为src或背景,并且9patch图像按应有的方式拉伸。 So I need to get a Bitmap this view contains and this Bitmap should not be linked somehow to a 9patch. 因此,我需要获取该视图包含的位图,并且不应以某种方式将此位图链接到9patch。 Thats why I encode bitmap to an byte-array and then decode it back - its just to get rid of 9patch. 这就是为什么我将位图编码为字节数组,然后将其解码回的原因-只是为了摆脱9patch。 If there is a more esay way to get a resulting bitmap from 9patch (some magic around NinePatchDrawable) - I would like to know about it. 如果还有一种更简便的方法可以从9patch获得结果位图(NinePatchDrawable周围的一些魔术)-我想知道这一点。
Ok, I figured out how to get rid of fake ImageView and make drawable to draw itself: all I have to do is to call setBounds()
method on a drawable: 好的,我想出了如何摆脱伪造的ImageView并使drawable进行绘制的方法:我要做的就是在drawable上调用setBounds()
方法:
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressDrawable.setBounds(0, 0, width, height);
mySeekBar.setProgressDrawable(progressDrawable);
Now I don't have to use ImageView, at last! 现在,我终于不必使用ImageView了!
However my code is a really long story to get rid of 9patch features in drawable. 但是,我的代码是一个很长的故事,要摆脱可绘制中的9patch功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.