我发现了下面的帖子如何设置imageview scaletype为topCrop
基于此,我使用了类TopCropImageView从https://gist.github.com/arriolac/3843346扩展ImageView
下面是我的布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.simha.quotes.TopCropImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView23"
android:src="@drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="New Text"
android:id="@+id/textView23"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="33dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold"
android:shadowColor="#000000"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="4" />
</RelativeLayout>
gradle编译没有任何错误。但是当我使用应用程序时,当我使用上面的布局时。它不再说"应用程序不幸停止了"之类的话。
有什么是缺失或有一个错误的方法。
我找到了解决方案:.java不使用2或3参数视图构造器;XML属性不起作用将这些添加到TopCropImageView的代码中,然后它就工作了。
public TopCropImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.MATRIX);
}
public TopCropImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setScaleType(ScaleType.MATRIX);
}