level 11
mport android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.*;
import android.view.*;
public class MoveView extends View
{
public MoveView(Context c)
{
super(c);
p=new Paint();
p.setDither(true);
p.setAntiAlias(true);
}
int alpla=0;
boolean isdo=false;
double aa=0;
Paint p;
boolean cj=false;
float x,y;
onMoveListener o;
public MoveView(Context c,AttributeSet a)
{
super(c,a);
p=new Paint();
p.setDither(true);
p.setAntiAlias(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
// TODO: Implement this method
super.onSizeChanged(w, h, oldw, oldh);
x=getWidth()/2f;
y=getHeight()/2f;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
isdo=true;
x=getWidth()/2f;
y=getHeight()/2f;
aa= Math.atan2( event.getX()-x,event.getY()-y)-Math.PI/2f;
if(o!=null)
o.start();
MoveView.this.invalidate();
if(alpla<250&!cj)
d(true);
else if(cj)
{
cj=false;
d(true);
}
break;
case MotionEvent.ACTION_MOVE:
isdo=true;
aa= Math.atan2( event.getX()-x,event.getY()-y)-Math.PI/2f;
if(o!=null)
o.move(aa);
MoveView.this.invalidate();
break;
case MotionEvent.ACTION_UP:
isdo=false;
x=getWidth()/2f;
y=getHeight()/2f;
MoveView.this.invalidate();
if(o!=null)
{
o.stop();
}
if(cj)
cj=false;
d(false);
break;
}
return true;
}
final Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
MoveView.this.invalidate();
}
};
public void d(final boolean b)
{
new Thread(new Runnable(){
@Override
public void run()
{
try
{
Thread.sleep(80);
}
catch (Exception e)
{}
if(b)
{
cj=true;
while(alpla<240&cj)
{
try
{
Thread.sleep(40);
alpla+=20;
2019年10月20日 16点10分
10
level 11
myHandler.sendMessage(myHandler.obtainMessage(22,"消息内容"));
}
catch (Exception e)
{}
}
if(cj)
cj=false;
}
}
}).start();
}
public void setonMoveLisener(onMoveListener i)
{
o=i;
}
@Override
protected void onDraw(Canvas canvas)
{
p.setColor(Color.GRAY);
p.setAlpha(30+(int)(alpla*0.8f));
p.setStyle(Paint.Style.FILL);
canvas.drawCircle(getWidth()/2f,getHeight()/2f,getWidth()/2f,p);
p.setColor(Color.BLUE);
p.setAlpha(30+(int)(alpla*0.8f));
if(isdo)
canvas.drawCircle(getWidth()/2f
+3
*getWidth()/8f*(float)Math.cos(aa),getHeight()/2f-3*getWidth()/8f*(float)Math.sin(aa),getWidth()/8f,p);
}
public interface onMoveListener
{
public void start();
public void move(double a);
public void stop();
}
}
2019年10月20日 16点10分
11