拉伸图像的宽度以填充布局android

时间:2015-03-08 06:59:49

标签: android android-imageview android-gridview

我有交错网格视图,其中每个项目都包含图像和文本。看看这个

enter image description here

下面的代码是项目布局xml。 DynamicHeightImageView是扩展的ImageView,用于更改图像的高度。在这里,我试图设置

android:layout_width="fill_parent"

但我认为它不起作用。



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_card"
    android:orientation="vertical" >

    <com.etsy.android.grid.util.DynamicHeightImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:gravity="center" />


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="4dp"
        android:paddingLeft="8dp"
        android:textColor="#ED430F"
        android:paddingRight="8dp"
        android:paddingTop="4dp"
        android:textSize="15sp"
        android:typeface="sans" />

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:paddingBottom="8dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp"
        android:textSize="12sp"
        android:textColor="#848484"
        android:textStyle="italic" />

</LinearLayout>
&#13;
&#13;
&#13;

DynamicHeightImageView类具有以下代码

&#13;
&#13;
public class DynamicHeightImageView extends ImageView {

    private double mHeightRatio;
    public static float radius = 2.0f;  
    
    Path clipPath = new Path();
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
    
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public DynamicHeightImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
		if (Build.VERSION.SDK_INT < 18) {
			this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
    }

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public DynamicHeightImageView(Context context) {
        super(context);
		if (Build.VERSION.SDK_INT < 18) {
			this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
    }

    public void setHeightRatio(double ratio) {
        if (ratio != mHeightRatio) {
            mHeightRatio = ratio;
            requestLayout();
        }
    }

    public double getHeightRatio() {
        return mHeightRatio;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mHeightRatio > 0.0) {
            // set the image views size
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) (width * mHeightRatio);
            setMeasuredDimension(width, height);
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    
	@Override
	protected void onDraw(Canvas canvas) {
		rect.left = 0;
		rect.top = 0;
		rect.right = this.getWidth();
		rect.bottom = this.getHeight();
		clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
		canvas.clipPath(clipPath);
		super.onDraw(canvas);
	}
}
&#13;
&#13;
&#13;

下面的代码是Grid View Adapter类。这实际上是与GridView绑定数据。在这里我也尝试设置图像视图宽度来填充父级,但它不是工作结果是相同的。任何人都可以告诉我我犯错的地方吗?

&#13;
&#13;
public class DataAdapter extends ArrayAdapter<Video> {
	
	Activity activity;
	int resource;
	List<Video> datas;

	public DataAdapter(Activity activity, int resource, List<Video> objects) {
		super(activity, resource, objects);

		this.activity = activity;
		this.resource = resource;
		this.datas = objects;
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final DealHolder holder;
		
		if (row == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			row = inflater.inflate(resource, parent, false);
			
			holder = new DealHolder();
			holder.image = (DynamicHeightImageView)row.findViewById(R.id.image);
			holder.title = (TextView)row.findViewById(R.id.title);
			holder.date = (TextView)row.findViewById(R.id.description);
			
			row.setTag(holder);
		}
		else {
			holder = (DealHolder) row.getTag();
		}
		
		final Video data = datas.get(position);
		Picasso.with(this.getContext())
				.load(data.getImgURL())
				.into(holder.image);
		
		holder.image.setHeightRatio(getRandomHeight());
		holder.image.getLayoutParams().width = LayoutParams.FILL_PARENT;
		holder.image.requestLayout();
		holder.title.setText(data.getTitle());
		holder.date.setText(data.getUploadedDate().toGMTString().subSequence(0, 16));
		
		return row;
	}
	
	static class DealHolder {
		DynamicHeightImageView image;
		TextView title;
		TextView date;
	}
	
	private float getRandomHeight(){
		
		
		ArrayList<Float>  lista = new ArrayList<Float>();
		lista.add((float) 0.5);
		lista.add((float) 1.0);
		lista.add((float) 0.75);
		lista.add((float) 1.5);
		
		Collections.shuffle(lista);
		return lista.get(0);
		
	}

}
&#13;
&#13;
&#13;

0 个答案:

没有答案