import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/**
* Created by 折扣卷布局
*/
public class CouponsView extends LinearLayout {
private Paint mPaint;
/**
* 圆间距
*/
private float gap = 8;
/**
* 半径
*/
private float radius = 10;
/**
* 圆数量
*/
private int circleNum;
private float remain;
public CouponsView(Context context) {
super(context);
}
public CouponsView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (remain == 0) {
remain = (int) (w - gap) % (2 * radius + gap);
}
circleNum = (int) ((w - gap) / (2 * radius + gap));
}
public CouponsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i=0;i<circleNum;i++){
float x = gap+radius+remain/2+((gap+radius*2)*i);
canvas.drawCircle(x,0,radius,mPaint);
canvas.drawCircle(x,getHeight(),radius,mPaint);
}
}
}
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:layout_centerInParent="true"
android:background="@android:color/white">
<com.zhy.magicviewpager.sample.CouponsView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/accent_material_light"
android:padding="20dp">
<ImageView
android:layout_width="120dp"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="豪华游卷"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:padding="5dp"
android:text="编号:2016042304561456"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:padding="5dp"
android:text="编号:2016042304561456"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="截止日期:2016-10-05"
/>
</LinearLayout>
</com.zhy.magicviewpager.sample.CouponsView>
</FrameLayout>