Trying to use custom imageview (topcrop) in android, but the the app stops

偶尔善良 提交于 2019-12-11 03:31:18

问题


I found the following post How set imageview scaletype to topCrop

Based on that i used the class TopCropImageView from https://gist.github.com/arriolac/3843346 which extends ImageView

The following is my layout xml file

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

The gradle compiles without any errors. But when i use the app, and when i come to the point of using the above layout. it stops saying something like "the app as to stop unfortunately."

Is there anything which is missing or has a wrong approach.


回答1:


i found the solution: .java not using the 2- or 3-argument View constructors; XML attributes will not work added these to the code of TopCropImageView then it worked.

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);
}


来源:https://stackoverflow.com/questions/32359213/trying-to-use-custom-imageview-topcrop-in-android-but-the-the-app-stops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!