使用ClipDrawable隐藏的一部分,ImageView的ClipDrawable、ImageView

2023-09-07 00:23:37 作者:Serious(高冷)

我试图隐藏图像的一部分,使用户不会看到它。起初,我拷贝到另一个位图位图的像素,而不复制只是我需要的像素,使得第二位图在创建正确的尺寸。这工作,但我有很多大型图像和奥姆斯导致不幸。所以不是这样做,我认为在使用ClipDrawable绘制图像,并使我不需要隐形的像素。

在code是如下:

  ClipDrawable clipDrawable =新ClipDrawable(新BitmapDrawable(资源,位图),重力方向);clipDrawable.setLevel(水平);//不能作为ImageView的源使用。必须使用的背景,否则我们没有得到任何屏幕上。picture.setBackground(clipDrawable);//这是超级重要的。不要修改此!没有它,你不会得到全屏图像的工作和ImageView的将被删除//从父布局。picture.setImageResource(android.R.color.transparent); 

的想法是,我计算基于图像大小,这样我隐藏我不需要的象素的级别。而且它的工作。但我不明白为什么我需要使用

  picture.setBackground(clipDrawable);picture.setImageResource(android.R.color.transparent); 

而不是更加正常的:

  picture.setImageDrawable(clipDrawable); 
三星手机如何打开隐藏文件

如果我做第二个比较正常的例子吧我不明白在什么ImageView的,但如果我将它设置为背景,把一个透明图像过它,那么它的工作原理。因为我想用需要的图片设置为src和不作为背景变焦类来进一步控制ImageView的,我不能兼得,无论是我得到的ClipDrawable表现还是我去有图像缩放。

任何帮助将是AP preciated!

解决方案

  picture.setImageDrawable(新    ClipDrawable(新BitmapDrawable(资源,位图),重力方向));ClipDrawable clipDrawable =(ClipDrawable)picture.getDrawable();clipDrawable.setLevel(水平); 

I am trying to hide a part of an image so that the user does not see it. Initially I copied the Bitmap pixels on another Bitmap, without copying only the pixels that I needed and making the second bitmap the correct size at creation. That worked, but I have many large images and that results in OOMs unfortunately. So instead of doing that I thought on using a ClipDrawable to draw the image, and making the pixels that I don't need invisible.

The code is as follows

ClipDrawable clipDrawable = new ClipDrawable(new BitmapDrawable(resources, bitmap), gravity, orientation);
clipDrawable.setLevel(level);

// Cannot use as the imageview source. Must use background or else we don't get anything on the screen.
picture.setBackground(clipDrawable);
// This is super important. Do not modify this! Without it you will not get the fullscreen image working and the ImageView will be deleted
// from the parent layout.
picture.setImageResource(android.R.color.transparent);

The idea is that I calculate the level based on the image size so that I hide the pixels that I don't need. And it's working. Except I don't understand why I need to use

picture.setBackground(clipDrawable);

picture.setImageResource(android.R.color.transparent);

instead of the more normal:

picture.setImageDrawable(clipDrawable);

If I do the second more normal example then I don't get anything in the ImageView, but if I set it as a background and put a transparent image over it, then it works. Since I want to further manipulate the ImageView using a zooming class that needs the picture set as the src and not as background, I cannot have both, either I get the ClipDrawable showing or I get to have zoom on the image.

Any help would be appreciated!

解决方案

picture.setImageDrawable(new
    ClipDrawable(new BitmapDrawable(resources, bitmap), gravity, orientation
));

ClipDrawable clipDrawable = (ClipDrawable) picture.getDrawable();
clipDrawable.setLevel(level);