android 启动动画的渐变
一.启动动画的渐变:
以后可能会用到的启动动画的效果:
效果图:
主界面:
public class MainActivity extends Activity { private LandingView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); lv = new LandingView(this); // 将自定义视图显示在本界面 setContentView(lv); // 创建修改Logo透明度的异步任务 LoGoTask task = new LoGoTask(this,lv); // 执行异步任务 task.execute(); } }
异步图片渐变:
public class LoGoTask extends AsyncTask<Object, Integer, String> { // 当前的透明度取值 int alpha = 0; MainActivity main; private LandingView lv; public LoGoTask(MainActivity main,LandingView lv){ this.main = main; this.lv = lv; } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } /** * 异步任务执行成功后,自动跳转至应用程序主界面 * */ @Override protected void onPostExecute(String result) { Intent intent=new Intent(main, TestActivity.class); main.startActivity(intent); main.finish(); } /** * 修改Logo自定义视图图片透明度,并重绘 * */ @Override protected void onProgressUpdate(Integer... values) { int temp = values[0].intValue(); lv.redraw(temp); } /** * 异步任务,循环改变透明度取值 * */ @Override protected String doInBackground(Object... arg0) { // TODO Auto-generated method stub while (alpha < 255) { try { Thread.sleep(100); publishProgress(new Integer(alpha)); alpha += 5; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }
显示图片制作:
public class LandingView extends View{ private int alpha = 0; private Bitmap logo; private Context context; public LandingView(Context context) { super(context); this.context = context; logo = BitmapFactory.decodeResource(context.getResources(), R.drawable.background1); } public void redraw(int aplha){ this.alpha = aplha; invalidate(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // 清屏 canvas.drawColor(Color.WHITE); // 创建画笔对象 Paint p = new Paint(); // 设置画笔对象的透明度为当前透明度取值 p.setAlpha(alpha); WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm=new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(dm); int width2=dm.widthPixels; int height2=dm.heightPixels; // 在视图上利用画笔绘制Logo图像 RectF rect = new RectF(0,0,width2,height2); canvas.drawBitmap(logo, null, rect, p); } }
再来个进入界面:
public class TestActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
都统一用一个布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。