我想让Meme应用Android。我有一个自定义的ImageView。它的调整大小位图保持宽度和高度之间的比率
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);
}
}
}
我的布局xml使用此自定义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>
截取此布局
我想获取宽度自定义ImageView并设置TextView的宽度“@ + id / txt_BotText和”@ + id / txt_TopText =自定义ImageView的宽度
答案 0 :(得分:0)
我得到宽度ImageView
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int imgViewWidth = imgMeme.getWidth();
txtTopText.setWidth(imgViewWidth);
txtBotText.setWidth(imgViewWidth);
}