因為數(shù)據(jù)比較多猾漫,是22*25的,所以手機上比較擠:
感覺最麻煩的就是數(shù)據(jù)感凤,程序員的惰性思維告訴我可以使用for循環(huán)填充悯周,但是愚笨的大腦沒找到實現(xiàn)的方式,就一點點手打了:
沒有辦法陪竿,我就是這么強大(dog)
接下來就是自定義View的實現(xiàn)過程:
public class Heart365View extends View {
private int[][] array = null;
private int width = 0;
private int height = 30;
private int padding = 0;
private Paint boxPaint, textPaint, bitmapPaint, backPaint;
private Bitmap heartBitmap;
private List<Integer> list = new ArrayList<>();
public Heart365View(Context context) {
this(context, null);
}
public Heart365View(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public Heart365View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
//初始化畫筆 和 寬度 和數(shù)據(jù)庫數(shù)據(jù)
private void init() {
array = HeartArray.getArrays();
backPaint = new Paint();
backPaint.setColor(Color.parseColor("#FFC0CB"));
boxPaint = new Paint();
boxPaint.setColor(Color.parseColor("#FA8072"));
textPaint = new Paint();
textPaint.setColor(Color.parseColor("#2595e4"));
textPaint.setTextSize(15);
bitmapPaint = new Paint();
heartBitmap = Utils.drawable2Bitmap(getContext().getResources().getDrawable(R.mipmap.heart));
list.addAll(SqlUtils.getInstance(getContext()).getAllData());
initBoxWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//循環(huán)渲染界面
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int num = array[i][j];
//大于0的是正常天數(shù)
if (num > 0) {
//這條數(shù)據(jù)是否需要渲染背景禽翼,也就是有沒有存過
boolean exists = list.indexOf(num) != -1;
//如果數(shù)據(jù)存在,就畫背景
if (exists)
canvas.drawRect(j * width + padding, i * height, (j + 1) * width + padding, (i + 1) * height, backPaint);
//上方橫線
canvas.drawLine(j * width + padding, i * height, (j + 1) * width + padding, i * height, boxPaint);
//左豎
canvas.drawLine(j * width + padding, i * height, j * width + padding, (i + 1) * height, boxPaint);
//下方橫線
canvas.drawLine((j + 1) * width + padding, i * height, (j + 1) * width + padding, (i + 1) * height, boxPaint);
//右豎
canvas.drawLine(j * width + padding, (i + 1) * height, (j + 1) * width + padding, (i + 1) * height, boxPaint);
//畫文字
canvas.drawText(num + "", (j * width) + getPaddingLR(num + "") + padding, ((i + 1) * height) - 10, textPaint);
} else if (num < 0) {
//畫心
canvas.drawBitmap(heartBitmap, (j * width) + padding, (i) * height + 1, bitmapPaint);
}
}
}
}
//新增天數(shù) 刷新界面
public void update(int i) {
list.add(i);
invalidate();
}
//初始化每一格的寬度
void initBoxWidth() {
int windowWidth = Utils.getWindowWidth(getContext());
width = (windowWidth) / (array[0].length);
padding = (windowWidth - (width * array[0].length)) / 2;
Log.e("yxs", "獲取屏幕寬度:" + windowWidth + "---" + width + "---" + array[0].length + "----" + padding);
}
//使數(shù)組居中族跛,計算Padding
int getPaddingLR(String s) {
switch (s.length()) {
case 1:
return 12;
case 2:
return 6;
case 3:
return 2;
}
return 0;
}
}
數(shù)據(jù)庫操作:
public class SqlUtils extends SQLiteOpenHelper {
private SQLiteDatabase database;
private Cursor cursor;
private static SqlUtils utils;
public static SqlUtils getInstance(Context c) {
if (utils == null)
utils = new SqlUtils(c);
return utils;
}
public SqlUtils(@Nullable Context context) {
super(context, "heart.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists h(_id integer primary key autoincrement , num integer)");
}
public boolean add(int i) {
if (isExists(i)) {
Utils.showToast("數(shù)據(jù)已存在~");
return false;
} else {
database = getDatabase();
database.execSQL("insert into h (num) values(?)", new String[]{i + ""});
return true;
}
}
public boolean isExists(int i) {
database = getDatabase();
cursor = database.rawQuery("select * from h where num=?", new String[]{i + ""});
return cursor.moveToFirst();
}
public List<Integer> getAllData() {
database = getDatabase();
cursor = database.rawQuery("select * from h", new String[]{});
List<Integer> list = new ArrayList<>();
while (cursor.moveToNext()) {
int i = cursor.getInt(cursor.getColumnIndex("num"));
Log.e("yxs", "讀取數(shù)據(jù):" + i);
list.add(i);
}
return list;
}
public SQLiteDatabase getDatabase() {
if (database == null)
database = getWritableDatabase();
return database;
}
public void close() {
if (cursor != null)
cursor.close();
if (database != null)
database.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
使用方法闰挡,xml直接調(diào)用自定義View,Activity:
public class MainActivity extends AppCompatActivity {
private Heart365View heartView;
private SqlUtils sqlUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heartView = findViewById(R.id.Heart);
final EditText ed = findViewById(R.id.Ed);
sqlUtils = SqlUtils.getInstance(this);
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = Integer.parseInt(ed.getText().toString());
if (sqlUtils.add(i)) {
heartView.update(i);
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
sqlUtils.close();
}
}
點個贊哦親礁哄,我太難了