看到一本電子雜志上有遮罩層的效果,感覺很漂亮,以為很麻煩,搜索了很多關於android遮罩層的,也沒有得出一點思路,原來就是一個透明的效果,然后上面彈出的控件是透明或者半透明之類的,可以選擇顏色,還是#ARBG,其中A就是傳說中的透明色的值(可以根據需要設置透明的效果),廢話不多說了,發一個簡單的Demo吧,是我山寨的那本雜志的效果:(由於雜志內容主要是圖片,彈出層才是給出的文字信息,所以我猜測是用Gallery顯示的雜志內容)
xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="@+id/showGallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:spacing="0dip"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#86222222"
>
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/secondKillButton"
android:text="00000000"
android:textColor="#ff0000"
/>
<Button
android:id="@+id/unfoldButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="展開"
/>
</RelativeLayout>
</FrameLayout>
主要的代碼也很簡單,還有一個簡單的Adapter,有不理解的朋友可以看我之前的blog
- package oneRain.UpMagazine;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.Gallery;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class ShowActivity extends Activity
- {
- private int i = 1;
- private int pos = 0;
- private List<String> contents = null;
- private static final String DIR = "/mnt/sdcard/UpMagazine/2010/content/";
- //設置是否展開
- private boolean isFolded = true;
- //設置控件
- private FrameLayout layout = null;
- private Gallery showGallery = null;
- private Button unfoldButton = null;
- private TextView textView = null;
- private TextView titleTextView = null;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.show);
- initView();
- }
- @Override
- protected void onResume()
- {
- // TODO Auto-generated method stu
- super.onResume();
- isFolded = true;
- }
- //初始化
- private void initView()
- {
- contents = new ArrayList<String>();
- File dir = new File(DIR);
- File[] files = dir.listFiles();
- for(int i=0; i<files.length; i++)
- {
- contents.add(DIR + files[i].getName());
- }
- layout = (FrameLayout)findViewById(R.id.layout);
- unfoldButton = (Button)findViewById(R.id.unfoldButton);
- unfoldButton.setOnClickListener(new UnfoldClickListener());
- showGallery = (Gallery)findViewById(R.id.showGallery);
- showGallery.setOnItemSelectedListener(new GalleryOnItemSelectedListener());
- showGallery.setAdapter(new ShowAdapter());
- titleTextView = (TextView)findViewById(R.id.titleTextView);
- }
- //滑動監聽
- private class GalleryOnItemSelectedListener implements OnItemSelectedListener
- {
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3)
- {
- // TODO Auto-generated method stub
- pos = arg2 + 1;
- titleTextView.setText("第" + pos +"個主題");
- }
- public void onNothingSelected(AdapterView<?> arg0)
- {
- // TODO Auto-generated method stub
- }
- }
- //按鈕監聽,展開一個透明的顯示文本的遮擋層
- private class UnfoldClickListener implements OnClickListener
- {
- public void onClick(View v)
- {
- if(isFolded)
- {
- textView = new TextView(ShowActivity.this);
- textView.setTextColor(Color.BLUE);
- textView.setTextSize(20);
- textView.setText("滾滾長江東逝水,浪花淘盡英雄。/n" +
- "是非成敗轉頭空,/n" +
- "青山依舊在,幾度夕陽紅。/n" +
- "白發漁樵江渚上,慣看秋月春風。 /n" +
- "一壺濁酒喜相逢,/n" +
- "古今多少事,都付笑談中。");
- textView.setGravity(Gravity.CENTER);
- textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.FILL_PARENT));
- textView.setBackgroundColor(Color.parseColor("#86222222"));
- unfoldButton.setText("收回");
- isFolded = false;
- layout.addView(textView);
- }
- else
- {
- unfoldButton.setText("展開");
- isFolded = true;
- layout.removeView(textView);
- }
- }
- }
- private class ShowAdapter extends BaseAdapter
- {
- public int getCount()
- {
- // TODO Auto-generated method stub
- return contents.size();
- }
- public Object getItem(int position)
- {
- // TODO Auto-generated method stub
- return position;
- }
- public long getItemId(int position)
- {
- // TODO Auto-generated method stub
- return 0;
- }
- public View getView(int position, View convertView, ViewGroup parent)
- {
- // TODO Auto-generated method stub
- ImageView i = new ImageView(ShowActivity.this);
- Bitmap bm = BitmapFactory.decodeFile(contents.get(position));
- // i.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,
- // Gallery.LayoutParams.FILL_PARENT));
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- i.setImageBitmap(bm);
- return i;
- }
- }
- }
效果如下: