【问题标题】:(Android)How can I show a part of image?(Android)如何显示图像的一部分?
【发布时间】:2013-08-06 06:58:47
【问题描述】:

我有这样的图像->

我想这样表现->

然后我可以选择我能看到多少(0~1,0=看不到,1=全图,0.5=半图,....等,从用户输入读取)

怎么做?

我尝试使用android:scaleType,但它不起作用。

【问题讨论】:

标签: android image


【解决方案1】:

我最好的建议是用ClipDrawable 包裹你的BitmapDrawable

ClipDrawable 允许您为任何其他可绘制对象定义剪辑,因此不会绘制整个可绘制对象,而只会绘制其中的一部分。

这将如何工作?您的 ImageView 可以显示可通过setImageDrawable() 分配的可绘制对象。自然地,您会在此处放置图像位图的 BitmapDrawable。如果你先用 ClipDrawable 包装你的 BitmapDrawable,然后再分配给 ImageView,你应该没问题。

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/clip_source" />

这是clip_sourcedrawable:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/your_own_drawable"
    android:gravity="top" />

您可以通过在 ClipDrawable (clip_source) 上调用函数 setLevel() 来定义剪切量。级别 0 表示图像完全隐藏,级别 10000 表示图像完全显示。您可以在中间使用任何 int 值。

您必须在代码中设置关卡,因此您的代码应首先获得对 ClipDrawable 的引用。您可以通过在 ImageView 实例上运行 getDrawable() 来完成此操作。当您有对 ClipDrawable 的引用时,只需在其上运行 setLevel(5000)(或任何其他数字 0-10000)。

ImageView img = (ImageView) findViewById(R.id.imageView1);
mImageDrawable = (ClipDrawable) img.getDrawable();
mImageDrawable.setLevel(5000);

【讨论】:

  • 如何设置级别?请详细说明。
  • 太棒了。非常感谢。
  • 您先生,刚刚在 2020 年救了一个人的命。加油!
【解决方案2】:

这是实现此目的的另一种方法

public static Bitmap getScaleBitmap(Bitmap bitmap) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()/2);
        final RectF rectF = new RectF(rect);
        final float roundPx = 0;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

在下面一行

final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()/2);

根据需要设置高度..

享受:)

【讨论】:

  • 如何实现中心裁剪。
  • 你是指圆形还是矩形?
【解决方案3】:

我通过在标记中的 ImageView 上设置 scrollY 来实现这一点。从 android 的文档来看,如果您正在处理运行低于 API 14 的设备,显然这不是一个选项。另外,在我的情况下,我使用的是基于应用程序状态加载的固定大小的图像,所以它们是总是相同的大小。因此,我可以在标记中实现它。我意识到这种方法并不适合所有人。

我将图像包裹在一个只有图标一半高的布局中,并故意将图标的大小设置为实际大小。如果没有设置 scrollY,它只显示图像的上半部分。设置 scrollY 将其移动到我想要的位置 - 仅显示图像的下半部分。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/default_icon"
        android:scrollY="50dp" />

</LinearLayout>

因此,在我的示例中,它只显示图标的下半部分。

【讨论】:

  • 下方留白
【解决方案4】:

这是一个简单的方法。在您的主布局中为您的图像创建一个单独的布局。确保它是真实的。现在将您的图片的图像视图放在布局的一侧并使其填充父级。好的,现在制作一个空白图像并将该图像视图添加到布局的底部,并将顶部添加到布局的中心。只是弄乱图像视图的边距和大小,直到它看起来像你想要的那样。希望这会有所帮助。

【讨论】:

  • 第二个 inageview 例如可以是白色的。只是为了隐藏主图像视图的下半部分。
【解决方案5】:

将您的drawable转换为位图并裁剪以获取图像顶部以形成另一个位图,然后将其显示到您的图像视图中。

// to convert  drawable to bitmap
Bitmap resource = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.your_resource);
// to set width of bitmap to imageview's width if necessary,
                        int width = imageView.getMeasuredWidth();
// to crop the bitmap to specific width and height,
                            resource = Bitmap.createBitmap(resource, 0, 0, width, height);

【讨论】:

    【解决方案6】:

    这是将图像裁剪成两部分的代码

    public static Bitmap getScaleBitmap(Bitmap bitmap, int check) {
        final Bitmap toBeCropped = bitmap;
    
        final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inTargetDensity = 1;
        toBeCropped.setDensity(Bitmap.DENSITY_NONE);
        int top = 0;
        int bottom = 0;
        int targetheight = 0;
        if (check == 0) {// return 1st half of image 
            top = 0;
            bottom = bitmap.getHeight() / 2;
    
        } else {// return 2nd half of image 
            top = (bitmap.getHeight() / 2) - 10;
            bottom = (bitmap.getHeight() / 2) - 10;
    
        }
        int fromHere = (int) (toBeCropped.getHeight() * 0.5);
    
        Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, top, toBeCropped.getWidth(), bottom);
        return croppedBitmap;
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多