两图形的关系图:
MyPathView
public class MyPathView extends View {
private int width;
private int height;
private Paint mPaint;
private Path mPath;
private Paint mPaintPoint;
private int count=0;
private int size=0;
private boolean isAdd;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0x23:
invalidate();
//上下波动
if(isAdd){
size++;
if(size>5){
isAdd=false;
}
}else {
size--;
if(size<-5){
isAdd=true;
}
}
count++;
if(count>80){
count=0;
}
// invalidate();
handler.sendEmptyMessageDelayed(0x23, 100);
break;
}
super.handleMessage(msg);
}
};
public MyPathView(Context context) {
super(context);
}
public MyPathView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mPaint.setTextSize(20);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.argb(0xff,0x8c,0xf0,0x67));//绿色
mPath=new Path();
mPaintPoint=new Paint();
mPaintPoint.setStrokeWidth(5);
mPaintPoint.setColor(Color.argb(0xff,0xff,0xff,0xff));//白色
PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
mPaint.setXfermode(mode);
handler.sendEmptyMessage(0x23);
}
private Bitmap mBitmapBubble;
private Canvas mCanvasBit;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
mBitmapBubble=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
mCanvasBit=new Canvas(mBitmapBubble);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画线:三角形
// mPath.moveTo(100,100);
// mPath.lineTo(0,200);
// mPath.lineTo(200,200);
// mPath.close();//闭合,这句话也可换成mPath.lineTo(100,100)
// canvas.drawPath(mPath,mPaint);
//沿着路径写字
// mPath.addCircle(width/2,height/2,100, Path.Direction.CCW);//线内 CW:线外
// mPath.close();
// canvas.drawPath(mPath,mPaint);
// canvas.drawTextOnPath("qqqqqqqqqqqqqqqqqqqqqqqqqwoooooooooooooooeoooooooo",mPath,0,0,mPaint);
//贝塞尔曲线:三个点就可确定一条贝塞尔曲线
// mPath.moveTo(100,100);
// mPath.quadTo(100,200,300,300);//控制点
// canvas.drawPath(mPath,mPaint);
//画出这三个点
// canvas.drawPoint(100,100,mPaintPoint);
// canvas.drawPoint(100,200,mPaintPoint);
// canvas.drawPoint(300,300,mPaintPoint);
//模拟加速球
canvas.drawColor(Color.argb(0xff,0x00,0x00,0x00));//背景色:黑色
mCanvasBit.drawCircle(300,300,150,mPaintPoint);
mPath.reset();
mPath.moveTo(500,200);
mPath.lineTo(500,500);
mPath.lineTo(count,500);
mPath.lineTo(count,200);
for(int i=0;i<10;i++){
mPath.rQuadTo(20,size,40,0);//rQuadTo每次以当前的一个点为原点
mPath.rQuadTo(20,-size,40,0);
}
mPath.close();
mCanvasBit.drawPath(mPath, mPaint);
canvas.drawBitmap(mBitmapBubble,0,0,null);
}
}
画线:三角形
沿着路径写字:
贝塞尔曲线:三个点就可确定一条贝塞尔曲线
模拟加速球
MyBitmapView
public class MyBitmapView extends View {
private int width;
private int height;
private Bitmap mBitmap;
private Paint mPaint;
private int bitmapWidth;
private int bitmapHeight;
private Matrix mMatrix;
public MyBitmapView(Context context) {
super(context);
}
public MyBitmapView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mBitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.view1);
bitmapWidth=mBitmap.getWidth();
bitmapHeight=mBitmap.getHeight();
Log.d("BITMAP","图片的信息:"+mBitmap.getWidth()+"x"+mBitmap.getHeight());
mMatrix=new Matrix();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//放大
mMatrix.reset();
mMatrix.postScale(2,2);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
//平移
mMatrix.reset();
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
mMatrix.postTranslate(0,bitmapHeight*2);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
//旋转
mMatrix.reset();
mMatrix.postRotate(180);
mMatrix.postTranslate(bitmapWidth*2,bitmapHeight*3);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
//关于x轴对称
mMatrix.reset();
float matrix_values[]={1f,0f,0f,0f,-1f,0f,0f,0f,1f};
mMatrix.setValues(matrix_values);
mMatrix.postTranslate(0,bitmapHeight*4);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
//关于y轴对称
mMatrix.reset();
float matrix_values2[]={-1f,0f,0f,0f,1f,0f,0f,0f,1f};
mMatrix.setValues(matrix_values2);
mMatrix.postTranslate(bitmapWidth*2,0);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
}
}
BitmapView
public class BitmapView extends View {
private int width;
private int height;
private Bitmap mBitmap;
private Paint mPaintCircel;
private Paint mPaintRect;
private Canvas canvasBit;
public BitmapView(Context context) {
super(context);
}
public BitmapView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintCircel=new Paint();
mPaintRect=new Paint();
mPaintCircel.setColor(Color.BLUE);//圆形设置为蓝色
mPaintRect.setColor(Color.YELLOW);//矩形设置为黄色
PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER);//蓝色被覆盖 DST_OVER:黄色被覆盖
mPaintRect.setXfermode(mode);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
mBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
canvasBit=new Canvas(mBitmap);//必须画在Bitmap上
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvasBit.drawCircle(width/2,height/2,width/2,mPaintCircel);
canvasBit.drawRect(0,0,width/2,height/2,mPaintRect);
canvas.drawBitmap(mBitmap,0,0,null);
}
}
MySliderView
public class MySliderView extends View {
private int width;
private int height;
private float x;
private float y;
int index=-1;
private Paint mPaintText;
private Paint mPaintColor;
private String[] array={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
//观察者模式
public interface onItemSelect{
public void onItemSelected(int index,String indexString);
}
private onItemSelect listener;
public void setOnItemSelectListener(onItemSelect listener){
this.listener=listener;
}
public MySliderView(Context context) {
super(context);
}
public MySliderView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintText=new Paint();
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintColor=new Paint();
mPaintColor.setColor(Color.RED);//设置字母颜色为红色
mPaintColor.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
mPaintText.setTextSize(height/26f);//设置每个字母的高度
mPaintColor.setTextSize(height/26f);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
//得到触碰的字母
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
x=event.getX();
y=event.getY();
if(x>width-mPaintText.measureText("m")*2){
index= (int) (y/(height/26f));
if(listener!=null){
listener.onItemSelected(index,array[index]);
}
Log.d("index","点击的是:"+array[index]);
invalidate();
return true;
}
break;
case MotionEvent.ACTION_UP:
index=-1;
invalidate();
break;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i=0;i<array.length;i++){
if(index==i){
canvas.drawText(array[i],width-mPaintText.measureText("m"),height/26f*(i+1),mPaintColor);
}else
canvas.drawText(array[i],width-mPaintText.measureText("m"),height/26f*(i+1),mPaintText);
}
}
}
MainActivity:
public class MainActivity extends Activity {
private TextView mTextView;
private MySliderView mySliderView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myslider);
mTextView= (TextView) findViewById(R.id.textview);
mySliderView= (MySliderView) findViewById(R.id.mysliderview);
mySliderView.setOnItemSelectListener(new MySliderView.onItemSelect() {
@Override
public void onItemSelected(int index, String indexString) {
mTextView.setText(indexString);//打印点击的字母
}
});
}
}
显示y: