【代码】android 遮罩层效果

看到一本电子杂志上有遮罩层的效果,感觉很漂亮,以为很麻烦,搜索了很多关于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


[java] 
view plaincopy 
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;  
        }  
    }  
}  


效果如下:

https://i-blog.csdnimg.cn/blog_migrate/af516f49f537547b3e9c171eba2480c9.gif


https://i-blog.csdnimg.cn/blog_migrate/e85fcfe5bcac98c958813bd0cd404ff5.gif


本文出自 “清源教育” 博客,转载请注明此处,谢谢!欢迎登录清源教育官网,查看更多视频教程。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值