我是java和android的新手(一般来说是面向对象代码的新手)。 我正在开发我的第一个项目,它将是android锁定模式屏幕的一个克隆,它会为标记的每个点喷出一个数值。
我已设法处理触摸事件以将我的点更改为标记并在TextView中设置数值,但我无法弄清楚如何实现onDraw()以在每个标记点之间绘制线条。说实话,我真的不明白该方法的构建方式或传递方式。我正在寻找一个简洁的解释,甚至是一个我可以使用的dummie-proof教程。任何帮助表示赞赏。
我确定我的代码很丑陋而且远没有效率,这是我最终会清理的东西。这是我到目前为止所拥有的。
public class MainActivity extends Activity implements View.OnTouchListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set an onTouchListener to the RelativeLayout layoutLockWrapper
ViewGroup lockwrapper = (RelativeLayout) findViewById(R.id.layoutLockWrapper);
lockwrapper.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
//declaring the ImageViews
ImageView topleft = (ImageView) findViewById(R.id.ivTopLeft);
ImageView topmid = (ImageView) findViewById(R.id.ivTopMid);
ImageView topright = (ImageView) findViewById(R.id.ivTopRight);
ImageView midleft = (ImageView) findViewById(R.id.ivMidLeft);
ImageView midmid = (ImageView) findViewById(R.id.ivMidMid);
ImageView midright = (ImageView) findViewById(R.id.ivMidRight);
ImageView botleft = (ImageView) findViewById(R.id.ivBotLeft);
ImageView botmid = (ImageView) findViewById(R.id.ivBotMid);
ImageView botright = (ImageView) findViewById(R.id.ivBotRight);
// I have a ton of integers here to declare the coordinates of each imageview
// there's 48 of them so I will leave them out for readability
EditText etLockCode = (EditText) findViewById(R.id.etLockcode);
//strings to get the last value of etLockCode and update with an additional character
String s = etLockCode.getText().toString();
String s1 = s+"1";
String s2 = s+"2";
String s3 = s+"3";
String s4 = s+"4";
String s5 = s+"5";
String s6 = s+"6";
String s7 = s+"7";
String s8 = s+"8";
String s9 = s+"9";
//case switch for handling touch events on layoutLockWrapper
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
//logic for switching the buttons source when inside the move path
float tx = event.getX();
float ty = event.getY();
if (tx <= r1 & tx >= l1 & ty >= t1 & ty <= b1 & etLockCode.getTag() != 1) {
topleft.setImageResource(R.mipmap.checked);
etLockCode.setText(s1);
etLockCode.setTag(1);
} else if (tx <= r2 & tx >= l2 & ty >= t2 & ty <= b2 & etLockCode.getTag() != 2) {
topmid.setImageResource(R.mipmap.checked);
etLockCode.setText(s2);
etLockCode.setTag(2);
} else if (tx <= r3 & tx >= l3 & ty >= t3 & ty <= b3 & etLockCode.getTag() != 3) {
topright.setImageResource(R.mipmap.checked);
etLockCode.setText(s3);
etLockCode.setTag(3);
} else if (tx <= r4 & tx >= l4 & ty >= t4 & ty <= b4 & etLockCode.getTag() != 4) {
midleft.setImageResource(R.mipmap.checked);
etLockCode.setText(s4);
etLockCode.setTag(4);
} else if (tx <= r5 & tx >= l5 & ty >= t5 & ty <= b5 & etLockCode.getTag() != 5) {
midmid.setImageResource(R.mipmap.checked);
etLockCode.setText(s5);
etLockCode.setTag(5);
} else if (tx <= r6 & tx >= l6 & ty >= t6 & ty <= b6 & etLockCode.getTag() != 6) {
midright.setImageResource(R.mipmap.checked);
etLockCode.setText(s6);
etLockCode.setTag(6);
} else if (tx <= r7 & tx >= l7 & ty >= t7 & ty <= b7 & etLockCode.getTag() != 7) {
botleft.setImageResource(R.mipmap.checked);
etLockCode.setText(s7);
etLockCode.setTag(7);
} else if (tx <= r8 & tx >= l8 & ty >= t8 & ty <= b8 & etLockCode.getTag() != 8) {
botmid.setImageResource(R.mipmap.checked);
etLockCode.setText(s8);
etLockCode.setTag(8);
} else if (tx <= r9 & tx >= l9 & ty >= t9 & ty <= b9 & etLockCode.getTag() != 9) {
botright.setImageResource(R.mipmap.checked);
etLockCode.setText(s9);
etLockCode.setTag(9);
}
}
return true;
}
}