您好,登錄后才能下訂單哦!
有項(xiàng)目需求需要繪制多個(gè)圓圈,并且使用連續(xù)的數(shù)字對(duì)其排列起來(lái),也就是好多排的圓圈。
首先看一下效果圖:
一排設(shè)置為8個(gè),一共有53個(gè)的:
一排設(shè)值為5個(gè)的,一共有153個(gè):
可以根據(jù)總的個(gè)數(shù)和每排個(gè)數(shù)自動(dòng)調(diào)節(jié)圓圈的大小,并且根據(jù)傳入的監(jiān)聽事件作出不同的點(diǎn)擊效果。
思路很簡(jiǎn)單,首先需要畫一個(gè)圓出來(lái):
<?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>
然后創(chuàng)建TextView,設(shè)置TextView的背景為這個(gè)drawable即可。
可以根據(jù)屏幕的總寬度和每排需要放置的圓個(gè)數(shù),計(jì)算出每一個(gè)圓的合適大小,然后設(shè)置每個(gè)圓之間的margin為這個(gè)圓的十分之一,所以這個(gè)圓的最后大小應(yīng)該為平均值的十分之八。
然后使用LinearLayout布局,動(dòng)態(tài)加載多個(gè)TextView,每個(gè)TextView設(shè)置好對(duì)應(yīng)的屬性即可。
由于可能圓的個(gè)數(shù)過(guò)多,所以需要ScrollView來(lái)進(jìn)行嵌套,從而支持滑動(dòng)事件,需要注意的就是ScrollView下面只能有一個(gè)子節(jié)點(diǎn)。
完整代碼:
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; //全屏參數(shù) LinearLayout.LayoutParams MatchParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); //用來(lái)設(shè)置內(nèi)部LinearLayout的參數(shù),寬度適應(yīng)父布局,高度自動(dòng) LinearLayout.LayoutParams LinearLayoutParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //設(shè)置TextView的參數(shù) 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); //用來(lái)得到總屏幕的寬度 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); init(); } private void init() { mLinearLayout.setOrientation(LinearLayout.VERTICAL); mMainLinearLayout.setLayoutParams(MatchParams);//最外面的全屏 mScrollView.setLayoutParams(MatchParams);//滾動(dòng)的全屏 mLinearLayout.setLayoutParams(MatchParams);//內(nèi)容的全屏 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;//計(jì)算出多少排 int duoyu = count - row * column;//計(jì)算出多余的個(gè)數(shù) int totalWidth = mWindowManager.getDefaultDisplay().getWidth();//得到總的寬度 int width = totalWidth / column;//計(jì)算出每排的每個(gè)圓圈的平均寬度 int margin = (int) (width*0.1);//設(shè)置邊距為0.1 width = (int) (width * 0.8);//去掉左右邊距剩下的寬度 int num = 1;//從1開始計(jì)數(shù) 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++) {//一排繪制多少個(gè) 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);//設(shè)置監(jiān)聽事件 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); } }
代碼結(jié)構(gòu):
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。