Android学习篇章1-MyView

本文探讨了在Android应用中实现特定功能的代码实现过程,包括界面布局、事件监听、数据处理等方面,详细展示了如何使用Java语言进行开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
				}
				
			}
			
			
		}
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值