有项目需求需要绘制多个圆圈,并且使用连续的数字对其排列起来,也就是好多排的圆圈。
首先看一下效果图:
一排设置为8个,一共有53个的:
一排设值为5个的,一共有153个:
可以根据总的个数和每排个数自动调节圆圈的大小,并且根据传入的监听事件作出不同的点击效果。
思路很简单,首先需要画一个圆出来:
<?xml version="1.0" encoding="UTF-8"?
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false"
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" /
<stroke
android:width="1dp"
android:color="@android:color/black" /
<size android:width="10dp"
android:height="10dp" /
</shape
然后创建TextView,设置TextView的背景为这个drawable即可。
可以根据屏幕的总宽度和每排需要放置的圆个数,计算出每一个圆的合适大小,然后设置每个圆之间的margin为这个圆的十分之一,所以这个圆的最后大小应该为平均值的十分之八。
然后使用LinearLayout布局,动态加载多个TextView,每个TextView设置好对应的属性即可。
由于可能圆的个数过多,所以需要ScrollView来进行嵌套,从而支持滑动事件,需要注意的就是ScrollView下面只能有一个子节点。
完整代码:
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* Created by leafage on 2017/7/21.
*/
public class CircleView {
private LinearLayout mMainLinearLayout;
private ScrollView mScrollView;
private LinearLayout mLinearLayout;
private WindowManager mWindowManager;
private Context mContext;
//全屏参数
LinearLayout.LayoutParams MatchParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//用来设置内部LinearLayout的参数,宽度适应父布局,高度自动
LinearLayout.LayoutParams LinearLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置TextView的参数
LinearLayout.LayoutParams TextViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
public CircleView(Context context) {
mContext = context;
mMainLinearLayout = new LinearLayout(context);
mLinearLayout = new LinearLayout(context);
mScrollView = new ScrollView(context);
//用来得到总屏幕的宽度
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
init();
}
private void init() {
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mMainLinearLayout.setLayoutParams(MatchParams);//最外面的全屏
mScrollView.setLayoutParams(MatchParams);//滚动的全屏
mLinearLayout.setLayoutParams(MatchParams);//内容的全屏
mLinearLayout.setOrientation(LinearLayout.VERTICAL);//垂直放置
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public LinearLayout getLinearLayout(int column, int count, View.OnClickListener onClickListener) {
int row = count / column;//计算出多少排
int duoyu = count - row * column;//计算出多余的个数
int totalWidth = mWindowManager.getDefaultDisplay().getWidth();//得到总的宽度
int width = totalWidth / column;//计算出每排的每个圆圈的平均宽度
int margin = (int) (width*0.1);//设置边距为0.1
width = (int) (width * 0.8);//去掉左右边距剩下的宽度
int num = 1;//从1开始计数
TextViewParams.setMargins(margin, margin, margin, margin);
for (int i = 0 ; i < row; i++) {//一共绘制多少排
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setLayoutParams(LinearLayoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < column; j++) {//一排绘制多少个
final TextView textView = new TextView(mContext);
textView.setBackground(mContext.getResources().getDrawable(R.drawable.cricle));
textView.setHeight(width);
textView.setWidth(width);
textView.setGravity(Gravity.CENTER);
textView.setText(String.valueOf(num));
textView.setLayoutParams(TextViewParams);
textView.setOnClickListener(onClickListener);//设置监听事件
linearLayout.addView(textView);
num++;
}
mLinearLayout.addView(linearLayout);
}
//绘制剩下多余的一排
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setLayoutParams(LinearLayoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0 ; i< duoyu;i++) {
TextView textView = new TextView(mContext);
textView.setBackground(mContext.getResources().getDrawable(R.drawable.cricle));
textView.setHeight(width);
textView.setWidth(width);
textView.setGravity(Gravity.CENTER);
textView.setText(String.valueOf(num));
textView.setLayoutParams(TextViewParams);
textView.setOnClickListener(onClickListener);
linearLayout.addView(textView);
num++;
}
mLinearLayout.addView(linearLayout);
mScrollView.addView(mLinearLayout);
mMainLinearLayout.addView(mScrollView);
return mMainLinearLayout;
}
}
可以直接在Activity里面使用:
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
private LinearLayout mMainLayout;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CircleView circle = new CircleView(this);
mMainLayout = circle.getLinearLayout(8, 53, new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "ID:" + ((TextView)view).getText(), Toast.LENGTH_SHORT).show();
}
});
setContentView(mMainLayout);
}
}
代码结构:
以上就是本文的全部内容,希望对大家的学习有所帮助。