【代码】android 遮罩层效果

2024-06-19 04:18
文章标签 代码 android 效果 遮罩

本文主要是介绍【代码】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;  }  }  
}  


效果如下:

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


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


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

这篇关于【代码】android 遮罩层效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073978

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指