我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top|center"
android:background="@drawable/rounded_corner"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="350dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="270dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="@drawable/card_image"
android:layout_gravity="top"
android:layout_marginTop="0dp"/>
</FrameLayout>图像应该用矩形填充图像,并缩放图像以保持其纵横比。当我不移动视图时,它工作得很好:

但是,当我移动视图时,图像将超出其界限:

我正在使用这个图书馆移动卡片。
如何防止ImageView将其图像绘制到其边界之外?
发布于 2015-08-27 14:06:41
我在毕加索中使用这个库,如下所示。我修改了你提供的代码,以保持它的熟悉。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top|center"
android:background="@drawable/rounded_corner"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="350dp">
<!-- Note that I've deleted a lot of properties from the ImageView -->
<!-- This is because Picasso will do a lot of that for us -->
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="270dp"
android:src="@drawable/card_image"
/>
</FrameLayout>当您膨胀视图时,您可以使用findViewById查找视图,就像通常所做的那样。
ImageView imageView = (ImageView) inflatedView.findViewById(R.id.image_view);
// Here comes the Picasso bit
Picasso.with(getActivity()) // You have to provide a Context
.load(R.drawable.card_image) // Can be a Uri, File or String path as well
.into(imageView); // Use .into(ImageView view, Callback callback) to register a listener for the image loading.这将简单地将图像放入ImageView而不对其进行修改。您可以添加.centerCrop()、.fit() (下面的示例)和其他漂亮的方法来自动编辑图像到容器属性。由于您在示例中使用了centerCrop,所以我将不深入这些内容,但请参阅我在顶部提供的Picasso页面,以获得进一步的文档。
若要对图像进行中心裁剪或匹配,可以在load()和.into()之间添加方法
Picasso.with(getActivity())
.load(R.drawable.card_image)
.centerCrop()
.fit()
.into(imageView);Picasso的另一个重要特性是它如何处理缓慢或不成功的加载:您可以添加占位符和错误资源:
Picasso.with(getActivity())
.load(R.drawable.card_image)
.placeholder(R.drawable.loading_image)
.error(R.drawable.error_image
.into(imageView);祝好运!
https://stackoverflow.com/questions/32210536
复制相似问题