我想按比例调整布局中的小部件,但我不能。
我按比例和尺寸调整ViewPager或com.daimajia.slider.SliderLayout的大小,但ViewPager或com.daimajia.slider.SliderLayout的子节目将失去其高度。
我为这项工作创建了一个自定义小部件。 ImageView的代码示例:
package ...;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
public class SquareImageView extends android.support.v7.widget.AppCompatImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
int w = 0, h = 0;
if (getWidth() == 0 || getHeight() == 0) {
if (getMeasuredWidth() == 0 || getMeasuredHeight() == 0) {
return;
}
else {
w = getMeasuredWidth();
h = getMeasuredHeight();
}
return;
}
else {
w = getWidth();
h = getHeight();
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
final Rect rect = new Rect(0, 0, w, w);
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, rect, rect, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp,
(int) (bmp.getWidth() / factor),
(int) (bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final String color = "#BAB399";
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor(color));
canvas.drawCircle(radius / 2 + 0.7f, radius / 2 + 0.7f,
radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
我的代码不适用于此小部件: ViewPager,com.daimajia.slider.SliderLayout。
//--------------------------------------------------------
//--------------------------------------------------------
//------------> ***** Answer - Go Down ***** <------------
//--------------------------------------------------------
//------------> ***** Answer - Go Down ***** <------------
//--------------------------------------------------------
//------------> ***** Answer - Go Down ***** <------------
//--------------------------------------------------------
//--------------------------------------------------------
我找到了答案我的问题。
public class ViewPagerByRatioSize extends ViewPager {
Integer layout_ratioX = 0, layout_ratioY = 0;
public ViewPagerByRatioSize(Context context) {
super(context);
}
public ViewPagerByRatioSize(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ViewPagerByRatioSize, 0, 0);
try {
layout_ratioX = typedArray.getInteger(R.styleable.ViewPagerByRatioSize_layout_ratioXViewPager, 0);
layout_ratioY = typedArray.getInteger(R.styleable.ViewPagerByRatioSize_layout_ratioYViewPager, 0);
}
finally {
typedArray.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = getMeasuredWidth();
super.setMeasuredDimension(width, width / layout_ratioX * layout_ratioY);
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(width / layout_ratioX * layout_ratioY, MeasureSpec.AT_MOST));
}
}
此代码对我有用,但我更改了代码,因为我的应用程序无法从xml代码中获取值。
这个新代码:
public class ViewPagerByRatioSize extends ViewPager {
public ViewPagerByRatioSize(Context context) {
super(context);
}
public ViewPagerByRatioSize(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = getMeasuredWidth();
super.setMeasuredDimension(width, width / 3 * 2);
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(width / 3 * 2, MeasureSpec.AT_MOST));
}
}
永远成功:)