[英]How to get width Custom ImageView and set width textview = width imageView?
我想要将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的宽度
我有宽度ImageView
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int imgViewWidth = imgMeme.getWidth();
txtTopText.setWidth(imgViewWidth);
txtBotText.setWidth(imgViewWidth);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.