android 遮罩层效果

看到一本电子杂志上有遮罩层的效果,感觉很漂亮,以为很麻烦,搜索了很多关于android遮罩层的,也没有得出一点思路,原来就是一个透明的效果,然后上面弹出的控件是透明或者半透明之类的,可以选择颜色,还是#ARBG,其中A就是传说中的透明色的值(可以根据需要设置透明的效果),废话不多说了,发一个简单的Demo吧,是我山寨的那本杂志的效果:(由于杂志内容主要是图片,弹出层才是给出的文字信息,所以我猜测是用Gallery显示的杂志内容)

xml布局文件:

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:id=”@+id/layout”
  4.     android:layout_width=”fill_parent”
  5.     android:layout_height=”fill_parent”
  6.     >
  7.     <Gallery
  8.         android:id=”@+id/showGallery”
  9.         android:layout_width=”fill_parent”
  10.         android:layout_height=”fill_parent”
  11.         android:spacing=”0dip”
  12.         />
  13.     <RelativeLayout
  14.             android:orientation=”horizontal”
  15.             android:layout_width=”fill_parent”
  16.             android:layout_height=”wrap_content”
  17.             android:layout_gravity=”bottom”
  18.             android:background=”#86222222″
  19.             >
  20.             <TextView
  21.                 android:id=”@+id/titleTextView”
  22.                 android:layout_width=”wrap_content”
  23.                 android:layout_height=”wrap_content”
  24.                 android:layout_toRightOf=”@id/secondKillButton”
  25.                 android:text=”00000000″
  26.                 android:textColor=”#ff0000″
  27.                 />
  28.             <Button
  29.                 android:id=”@+id/unfoldButton”
  30.                 android:layout_width=”wrap_content”
  31.                 android:layout_height=”wrap_content”
  32.                 android:layout_alignParentRight=”true”
  33.                 android:text=”展开”
  34.                 />
  35.     </RelativeLayout>
  36. </FrameLayout>

主要的代码也很简单,还有一个简单的Adapter,有不理解的朋友可以看我之前的blog

[java][/java]

[java][/java] view plaincopy

  1. view plaincopy
  2. package oneRain.UpMagazine;
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import android.app.Activity;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.view.Gravity;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.view.ViewGroup;
  15. import android.view.Window;
  16. import android.widget.AdapterView;
  17. import android.widget.AdapterView.OnItemSelectedListener;
  18. import android.widget.BaseAdapter;
  19. import android.widget.Button;
  20. import android.widget.FrameLayout;
  21. import android.widget.Gallery;
  22. import android.widget.ImageView;
  23. import android.widget.TextView;
  24. public class ShowActivity extends Activity
  25. {
  26.     private int i = 1;
  27.     private int pos = 0;
  28.     private List<String> contents = null;
  29.     private static final String DIR = “/mnt/sdcard/UpMagazine/2010/content/”;
  30.     //设置是否展开
  31.     private boolean isFolded = true;
  32.     //设置控件
  33.     private FrameLayout layout = null;
  34.     private Gallery showGallery = null;
  35.     private Button unfoldButton = null;
  36.     private TextView textView = null;
  37.     private TextView titleTextView = null;
  38.     public void onCreate(Bundle savedInstanceState)
  39.     {
  40.         super.onCreate(savedInstanceState);
  41.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  42.         setContentView(R.layout.show);
  43.         initView();
  44.     }
  45.     @Override
  46.     protected void onResume()
  47.     {
  48.         // TODO Auto-generated method stu
  49.         super.onResume();
  50.         isFolded = true;
  51.     }
  52.     //初始化
  53.     private void initView()
  54.     {
  55.         contents = new ArrayList<String>();
  56.         File dir = new File(DIR);
  57.         File[] files = dir.listFiles();
  58.         for(int i=0; i<files.length; i++)
  59.         {
  60.             contents.add(DIR + files[i].getName());
  61.         }
  62.         layout = (FrameLayout)findViewById(R.id.layout);
  63.         unfoldButton = (Button)findViewById(R.id.unfoldButton);
  64.         unfoldButton.setOnClickListener(new UnfoldClickListener());
  65.         showGallery = (Gallery)findViewById(R.id.showGallery);
  66.         showGallery.setOnItemSelectedListener(new GalleryOnItemSelectedListener());
  67.         showGallery.setAdapter(new ShowAdapter());
  68.         titleTextView = (TextView)findViewById(R.id.titleTextView);
  69.     }
  70.     //滑动监听
  71.     private class GalleryOnItemSelectedListener implements OnItemSelectedListener
  72.     {
  73.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  74.                 long arg3)
  75.         {
  76.             // TODO Auto-generated method stub
  77.             pos = arg2 + 1;
  78.             titleTextView.setText(“第” + pos +”个主题”);
  79.         }
  80.         public void onNothingSelected(AdapterView<?> arg0)
  81.         {
  82.             // TODO Auto-generated method stub
  83.         }
  84.     }
  85.     //按钮监听,展开一个透明的显示文本的遮挡层
  86.     private class UnfoldClickListener implements OnClickListener
  87.     {
  88.         public void onClick(View v)
  89.         {
  90.             if(isFolded)
  91.             {
  92.                 textView = new TextView(ShowActivity.this);
  93.                 textView.setTextColor(Color.BLUE);
  94.                 textView.setTextSize(20);
  95.                 textView.setText(“滚滚长江东逝水,浪花淘尽英雄。/n” +
  96.                         “是非成败转头空,/n” +
  97.                         “青山依旧在,几度夕阳红。/n” +
  98.                         “白发渔樵江渚上,惯看秋月春风。 /n” +
  99.                         “一壶浊酒喜相逢,/n” +
  100.                         “古今多少事,都付笑谈中。”);
  101.                 textView.setGravity(Gravity.CENTER);
  102.                 textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
  103.                         ViewGroup.LayoutParams.FILL_PARENT));
  104.                 textView.setBackgroundColor(Color.parseColor(“#86222222”));
  105.                 unfoldButton.setText(“收回”);
  106.                 isFolded = false;
  107.                 layout.addView(textView);
  108.             }
  109.             else
  110.             {
  111.                 unfoldButton.setText(“展开”);
  112.                 isFolded = true;
  113.                 layout.removeView(textView);
  114.             }
  115.         }
  116.     }
  117.     private class ShowAdapter extends BaseAdapter
  118.     {
  119.         public int getCount()
  120.         {
  121.             // TODO Auto-generated method stub
  122.             return contents.size();
  123.         }
  124.         public Object getItem(int position)
  125.         {
  126.             // TODO Auto-generated method stub
  127.             return position;
  128.         }
  129.         public long getItemId(int position)
  130.         {
  131.             // TODO Auto-generated method stub
  132.             return 0;
  133.         }
  134.         public View getView(int position, View convertView, ViewGroup parent)
  135.         {
  136.             // TODO Auto-generated method stub
  137.             ImageView i = new ImageView(ShowActivity.this);
  138.             Bitmap bm = BitmapFactory.decodeFile(contents.get(position));
  139. //          i.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,
  140. //                  Gallery.LayoutParams.FILL_PARENT));
  141.             i.setScaleType(ImageView.ScaleType.FIT_XY);
  142.             i.setImageBitmap(bm);
  143.             return i;
  144.         }
  145.     }
  146. }

效果如下:

http://pic002.cnblogs.com/images/2012/274740/2012031710144442.gif

 

http://pic002.cnblogs.com/images/2012/274740/2012031710151518.gif

 

标签