大佬教程收集整理的这篇文章主要介绍了android – 如何根据小部件高度缩放图像视图但是尊重最大高度?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在开发一个包含图像视图的Android小部件.
该小部件应该可以调整大小.
图像应该填充小部件的整个宽度(这是容易的部分)并且应该填充小部件高度的30%,同时尊重最大高度80dip(这是我无法弄清楚的).
在以下布局中,图像适当缩放到宽度,但不适合高度:
<RelativeLayout xmlns:android="http://scheR_204_11845@as.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<ImageView
android:id="@+id/promoSHelf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sHelf"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我尝试使用带权重的LinearLayout来完成此操作,这可以在下面的代码片段中看到.但是,它看起来像android:maxHeight在使用权重时不受尊重.
<RelativeLayout xmlns:android="http://scheR_204_11845@as.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"
android:weight_sum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dip"
android:maxHeight="80dip"
android:src="@drawable/my_image"
android:scaleType="fitXY"
android:layout_weight="0.3" />
</LinearLayout>
</RelativeLayout>
所以,这是(不需要的)结果:
为什么maxHeight不起作用或者是否有人知道如何规避这个?
解决方法:
我不确定是否有一个@R_325_8749@组件的好解决方案.但是可以使用自定义ImageView小部件轻松完成.
public class MyImageView extends ImageView {
private float weight = 0.3f;
privatE int maxHeight;
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
final int[] ids = {android.R.attr.maxHeight};
final TypedArray array = context.obtainStyledAttributes(attrs, ids);
if (array != null) {
maxHeight = array.getDimensionPixelSize(0, 0);
array.recycle();
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpeC) {
if (maxHeight > 0) {
final int height = MeasureSpec.getSize(heightMeasureSpec);
final int result = Math.min(Math.round(height * weight), maxHeight);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(result, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
然后在你的布局中:
<RelativeLayout
xmlns:android="http://scheR_204_11845@as.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:BACkground="#0f0">
<com.example.MyImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:BACkground="#f00"
android:maxHeight="80dip"/>
</RelativeLayout>
以上是大佬教程为你收集整理的android – 如何根据小部件高度缩放图像视图但是尊重最大高度?全部内容,希望文章能够帮你解决android – 如何根据小部件高度缩放图像视图但是尊重最大高度?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。