简体   繁体   中英

How to get width Custom ImageView and set width textview = width imageView?

I want make Meme application Android. I have a custom ImageView. Its resize bitmap keep ratio between width and height

public class MemeImageView extends ImageView {

    public MemeImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        try {
            Drawable drawable = getDrawable();

            if (drawable == null) {
                setMeasuredDimension(0, 0);
            } else {
                float imageSideRatio = (float) drawable.getIntrinsicWidth() / (float) drawable.getIntrinsicHeight();
                float viewSideRatio = (float) MeasureSpec.getSize(widthMeasureSpec) / (float) MeasureSpec.getSize(heightMeasureSpec);
                if (imageSideRatio >= viewSideRatio) {
                    // Image is wider than the display (ratio)
                    int width = MeasureSpec.getSize(widthMeasureSpec);
                    int height = (int) (width / imageSideRatio);
                    setMeasuredDimension(width, height);
                } else {
                    // Image is taller than the display (ratio)
                    int height = MeasureSpec.getSize(heightMeasureSpec);
                    int width = (int) (height * imageSideRatio);
                    setMeasuredDimension(width, height);
                }
            }
        } catch (Exception e) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

My layout xml use this custom ImageView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.blogspot.bp_apps.memegeneratorbp.MemeMakerActivity">


<android.support.v7.widget.Toolbar
    android:id="@+id/tbar_top_MemeMaker"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="?attr/colorPrimary"
    android:minHeight="45dp"
    app:popupTheme="@style/AppTheme.PopupOverlay">


</android.support.v7.widget.Toolbar>


<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tbar_top_MemeMaker"
    android:stretchColumns="*">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <EditText
                android:id="@+id/edit_TextTop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="15"
                android:hint="@string/hint_top_text"
                android:maxLines="1"
                android:textSize="15dp" />

            <ImageButton
                android:id="@+id/btn_ConfigTopText"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:nestedScrollingEnabled="false" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/edit_TextBot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="@string/hint_bot_text"
                android:maxLines="1"
                android:textSize="15dp" />

            <ImageButton
                android:id="@+id/btn_ConfigBotText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>
</TableLayout>

<MemeCustomView.MemeTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/txt_MemeName"
    android:layout_below="@+id/tableLayout"
    android:layout_centerHorizontal="true"
    android:gravity="center" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/txt_Ads2"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/txt_MemeName">

    <MemeCustomView.MemeImageView
        android:id="@+id/imgV_Meme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true" />

    <MemeCustomView.MemeTextView
        style="@style/MemeTextStyle"
        android:id="@+id/txt_TopText"
        android:layout_alignTop="@+id/imgV_Meme"
        android:text="@string/hint_top_text"/>

    <MemeCustomView.MemeTextView
        style="@style/MemeTextStyle"
        android:id="@+id/txt_BotText"
        android:layout_alignBottom="@+id/imgV_Meme"
        android:text="@string/hint_bot_text"/>

</RelativeLayout>


<TextView
    android:id="@+id/txt_Ads2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tbar_bot_MemeMaker"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="Text Here"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30dp" />

<android.support.v7.widget.Toolbar
    android:id="@+id/tbar_bot_MemeMaker"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="?attr/colorPrimary"
    android:minHeight="45dp"
    app:popupTheme="@style/AppTheme.PopupOverlay">

</android.support.v7.widget.Toolbar>

Screenshot this layout

在此处输入图片说明

I want get width custom ImageView and set width of TextView "@+id/txt_BotText and "@+id/txt_TopText = width of custom ImageView

I has get width ImageView

  @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    int imgViewWidth = imgMeme.getWidth();

    txtTopText.setWidth(imgViewWidth);
    txtBotText.setWidth(imgViewWidth);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM