Clove的Android备课笔记之第一课 多图片浏览
要捣鼓Android了,整个备课笔记吧.这是第一篇,希望还有后续坚持下来.
效果图如下:
传完了才发现悲剧了,抓图是之前的,现在已经换成4个按钮了,唉.懒得动弹了
接下来介绍过程吧
step:
1.创建新Android项目,起名
Avtivity–>ChangPic
package–>clove.test
其他随意
2.弄一批大小一样的图片到res/drawable-mdpi下
例如我这里是p1.png~p8.png
3.准备按钮显示文字 打开res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">30c.org PicViewer</string> <string name="prov">上一张</string> <string name="next">下一张</string> <string name="addAlpha">增加透明度</string> <string name="subAlpha">减少透明度</string> </resources>
3.设计布局 打开res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal"> <ImageView android:id="@+id/showPic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/p1" ></ImageView> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/butProv" android:text="@string/prov" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:id="@+id/butAddAlpha" android:text="@string/addAlpha" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:id="@+id/butSubAlpha" android:text="@string/subAlpha" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:id="@+id/butNext" android:text="@string/next" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> </LinearLayout>
4.打开src/clove/test/ChangePic.java
package clove.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class ChangPic extends Activity { private Button butProv; private Button butAddAlpha; private Button butSubAlpha; private Button butNext; private ImageView showPic; private OnClickListener listener; private int[] allPic;//所有待显示图片 private int alpha = 255;//默认图片可见度最大 private int picOrder = 0;//当前显示的图片次序 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //初始化图片 allPic = new int[]{ R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4, R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8 }; //得到对象 butProv = (Button) findViewById(R.id.butProv); butAddAlpha = (Button) findViewById(R.id.butAddAlpha); butSubAlpha = (Button) findViewById(R.id.butSubAlpha); butNext = (Button) findViewById(R.id.butNext); showPic = (ImageView) findViewById(R.id.showPic); //创建匿名内部监听对象 listener = new View.OnClickListener() { public void onClick(View v) { if(v==butProv)//上一张,其实可以到头循环,为了测试信息框就判断吧 { if(picOrder>0) showPic.setImageResource(allPic[--picOrder]); else Toast.makeText(ChangPic.this, "前面已经木有图片啦", 5000).show(); } else if(v==butNext)//下一张,到头自动循环 { if(picOrder==allPic.length-1) Toast.makeText(ChangPic.this, "眼一闭一睁,一圈结束了", 5000).show(); picOrder = ++picOrder%allPic.length; showPic.setImageResource( allPic[picOrder]); } else if(v==butAddAlpha) { alpha+=alpha>=255?0:25; showPic.setAlpha(alpha); } else if(v==butSubAlpha) { alpha-=alpha<=0?0:25; showPic.setAlpha(alpha); } } }; //添加监听器注册 butProv.setOnClickListener(listener); butNext.setOnClickListener(listener); butAddAlpha.setOnClickListener(listener); butSubAlpha.setOnClickListener(listener); } }
还有张代码图,一起发了吧
打完收工,木哈哈哈
分享到: | |
没有评论