android自己写的类似刻度尺的东西
刚学android不久,因为公司项目要求,写了个类似刻度尺的东西,拿出来献丑,希望大家给点意见。
先上代码,注:KeduView中的浮点数计算我没处理(因为精度问题,浮点数直接计算出来的结果可能不对)。StaffView中的浮点数计算我进行了处理,我在Arithmetic中封装了加减乘除方法:
Java代码
package com.hyx.suiyipaint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class KeduActivity extends Activity {
private ImageView kedu_tiao;
private LinearLayout kedu_linear;
private LinearLayout staff_linear;
private KeduView kedu;
private StaffView staff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kedu);
kedu_linear = (LinearLayout)findViewById(R.id.kedu_linear);
kedu = new KeduView(this, 0f, 0.1f);
kedu_linear.addView(kedu);
staff_linear = (LinearLayout)findViewById(R.id.staff_linear);
staff = new StaffView(this, 7.5f, 0.5f, "cm");
staff_linear.addView(staff);
kedu_tiao = (ImageView)findViewById(R.id.kedu_tiao);
kedu_tiao.setOnTouchListener(keduListener);
}
private ImageView.OnTouchListener keduListener = new ImageView.OnTouchListener(){
private float initx = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
initx = event.getX();
break;
case MotionEvent.ACTION_MOVE:
float lastx = event.getX();
if(lastx > initx + 5){
kedu.draw(1);
initx = lastx;
}else if(lastx < initx -5){
kedu.draw(-1);
initx = lastx;
}
break;
}
return true;
}
};
class KeduView extends Su易做图ceView implements Su易做图ceHolder.Callback, Runnable{
private Su易做图ceHolder mSu易做图ceHolder = null;
private Canvas canvas;
//画布背景
private Bitmap background;
//刻度游标
private Bitmap pointer;
//总刻度数
private static final int KEDU_COUNT = 25;
//刻度最小值
private float init_min;
//每个刻度的单位值
private float interval;
public KeduView(Context context, float init_min, float interval) {
super(context);
mSu易做图ceHolder = this.getHolder();
mSu易做图ceHolder.addCallback(this);
this.setFocusable(true);
background = BitmapFactory.decodeResource(getResources(), R.drawable.kedu_bg);
pointer = BitmapFactory.decodeResource(getResources(), R.drawable.kedu_pointer);
this.init_min = init_min;
&n
补充:移动开发 , Android ,