public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
MyView view =new MyView(this);
setContentView(view);
// view.s
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class MyView extends View{
Context context;
int alpha = 255;
int rowx=320;
int coly=20;
float strlength=0;
String [] strs=new String []{"天若有情天亦老,人若有情死得早!","人生就是一个茶几!","上面充满了杯具!"};
int index=0;
int red=255;
int green=0;
int blue=0;
public MyView(Context context) {
super(context);
this.context=context;
MyViewThread thread= new MyViewThread();
thread.start();
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
canvas.drawColor(Color.YELLOW);
Paint paint= new Paint();
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(4);
paint.setColor(Color.rgb(255, 0, 0));
canvas.drawLine(10, 10, 300, 300, paint);
paint.setStyle(Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.argb(alpha, 255, 0, 0));
canvas.drawCircle(200, 200, 50, paint);
paint.setTextSize(20);
paint.setColor(Color.argb(alpha, red, green, blue));
String str=strs[index];
strlength=paint.measureText(str);
canvas.drawText(str, rowx, coly, paint);
}
public class MyViewThread extends Thread
{
@Override
public void run() {
while(true)
{
try {
Thread.sleep(100);
alpha= alpha-10;
rowx=rowx-10;
if(rowx+strlength<0)
{
rowx=320;
index++;
coly+=30;
if(coly>=420)
{
coly=20;
}
if(index==strs.length)
{
index=0;
}
}
if(alpha==5)
{
alpha=255;
}
red=red-25;
green=green+25;
blue=blue+25;
if(red<=0){red=255;}
if(green>=255){green=0;}
if(blue>=255){blue=0;}
postInvalidate();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}