(安卓)我怎么能显示图像的一部分吗? [英] (Android)How can I show a part of image?

查看:115
本文介绍了(安卓)我怎么能显示图像的一部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的图像 - >

I have an image like this->

和我想给这样的 - >

And I want to show like this->

然后,我可以选择我多少可以看到(0〜1,0 =不能看,1 =全貌,0.5 =半个画面,....等,从用户的输入读取)

Then I can choose how much I can see(0~1,0=can not see,1=Whole picture,0.5=half picture,....etc,read from user input)

如何做到这一点?

我试图用安卓scaleType ,但它不能正常工作

I tried to use android:scaleType but it is not working.

推荐答案

我的最好的建议是要包装你的<一个href="http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html">BitmapDrawable以<一href="http://developer.android.com/reference/android/graphics/drawable/ClipDrawable.html">ClipDrawable.

My best suggestion is to wrap your BitmapDrawable with a ClipDrawable.

ClipDrawable让你定义裁剪任何其他绘制,所以不是绘制整个绘制的,它只是一个部分将被绘制。

ClipDrawable lets you define clipping for any other drawable, so instead of drawing the entire drawable, only a part of it will be drawn.

如何将这项工作?您的ImageView可以显示一个绘制 - 分配到 setImageDrawable()。当然,你也把你的位图图像的BitmapDrawable那里。如果您有ClipDrawable第一包住BitmapDrawable,然后才分配给ImageView的,你应该罚款。

How would that work? Your ImageView can display a drawable - assignable through setImageDrawable(). Naturally, you would place a BitmapDrawable of your image bitmap there. If you wrap your BitmapDrawable first with ClipDrawable, and only then assign to the ImageView, you should be fine.

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

和这是 clip_source 绘制:

<?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" />

您可以通过调用函数执行setLevel()您ClipDrawable定义剪切量( clip_source )。 0级表示图像被完全隐藏和10000水平意味着图像完全显现出来。您可以在中间使用任何int值。

You can define the amount of clipping by calling the function setLevel() on your ClipDrawable (clip_source). A level of 0 means the image is completely hidden and a level of 10000 means the image is completely revealed. You can use any int value in the middle.

您将不得不设置在code的水平,所以你的code应该先得到一个参考ClipDrawable。您可以在您的ImageView实例中运行的 getDrawable()做到这一点。当你有一个关于你ClipDrawable,只需运行执行setLevel(5000)就可以了(或任何其他数字0-10000)。

You'll have to set the level in code, so your code should first get a reference to the ClipDrawable. You can do this by running getDrawable() on your ImageView instance. When you have a reference to your ClipDrawable, simply run setLevel(5000) on it (or any other number 0-10000).

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

这篇关于(安卓)我怎么能显示图像的一部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆