Android 365存钱法 心形自定义View实现
2020-04-15 本文已影响0人
坐怀灬不乱
因为数据比较多,是22*25的,所以手机上比较挤:
b4ecf83ebf62decad48ea827d4df56e.jpg
感觉最麻烦的就是数据,程序员的惰性思维告诉我可以使用for循环填充,但是愚笨的大脑没找到实现的方式,就一点点手打了:
image.png
没有办法,我就是这么强大(dog)
接下来就是自定义View的实现过程:
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();
}
//初始化画笔 和 宽度 和数据库数据
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);
//循环渲染界面
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int num = array[i][j];
//大于0的是正常天数
if (num > 0) {
//这条数据是否需要渲染背景,也就是有没有存过
boolean exists = list.indexOf(num) != -1;
//如果数据存在,就画背景
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);
}
}
}
}
//新增天数 刷新界面
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);
}
//使数组居中,计算Padding
int getPaddingLR(String s) {
switch (s.length()) {
case 1:
return 12;
case 2:
return 6;
case 3:
return 2;
}
return 0;
}
}
数据库操作:
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("数据已存在~");
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", "读取数据:" + 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直接调用自定义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();
}
}
点个赞哦亲,我太难了