微博项目里要求实现这种效果,里面的图片随机从网络获取,外面的灰色边框需要代码添加上去。
参照网上的代码,北京为ImageView,通过FrameLayout实现两个ImageView上下显示。并将下面的ImageView北京设置为白色,并添加边框。
上代码:
XML文件:
<FrameLayout
android:layout_width="50.0dip"
android:layout_height="50.0dip"
android:layout_weight="0.0"
android:layout_marginLeft="5.0dip"
android:background="#888888"
>
<!-- android:background="@drawable/bg15" -->
<com.cyol.android.wbxy.mylistview.myImageView
android:id="@+id/home_headicon_bg"
android:layout_width="60.0dip"
android:layout_height="60.0dip"
android:scaleType="center"
android:background="@color/white"
/>
<ImageView
android:id="@+id/home_headicon"
android:layout_width="45.0dip"
android:layout_height="45.0dip"
android:layout_gravity="center"
android:src="@drawable/touxiang_icon"
android:scaleType="centerInside" />
</FrameLayout>
自定义ImageView:
package com.cyol.android.wbxy.mylistview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;
public class myImageView extends ImageView {
private int co;
private int borderwidth;
public myImageView(Context context) {
super(context);
}
public myImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public myImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//设置颜色
public void setColour(int color){
co = color;
}
//设置边框宽度
public void setBorderWidth(int width){
borderwidth = width;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 画边框
Rect rec = canvas.getClipBounds();
// rec.bottom;
// rec.right--;
Paint paint = new Paint();
//设置边框颜色
paint.setColor(co);
paint.setStyle(Paint.Style.STROKE);
//设置边框宽度
paint.setStrokeWidth(borderwidth);
canvas.drawRect(rec, paint);
}
}
代码调用:
home_headicon_bg=(myImageView)rowView.findViewById(R.id.home_headicon_bg);
home_headicon_bg.setColour(Color.GRAY);
home_headicon_bg.setBorderWidth(2);
// 头像图片
home_headicon = (ImageView) rowView .findViewById(R.id.home_headicon);