Android仿新浪微博发送菜单界面的实现

作者:BertSir 时间:2022-12-13 10:54:03 

效果图

Android仿新浪微博发送菜单界面的实现

接下来就是一波贴代码的过程

自定义Dialog


public class SinaSendView extends Dialog {
 private ImageButton ib_dialog_sina_close;
 private LinearLayout ll_dialog_sina_write;
 private LinearLayout ll_dialog_sina_time;
 private LinearLayout ll_dialog_sina_map;
 private LinearLayout ll_dialog_sina_menu;
 private ImageView iv_dialog_sina_bg,iv_dialog_sina_des;
 private Context mContext;
 private Boolean hideDes;
 private Bitmap screenShot;
 private Bitmap bitmap;
 private ByteArrayOutputStream baos;
 private byte[] bytes;

public SinaSendView(Context context) {
   super(context);
   this.mContext = context;
 }

public SinaSendView(Context context, int themeResId,Boolean hideDes) {
   super(context, themeResId);
   this.mContext = context;
   this.hideDes = hideDes;
 }

protected SinaSendView(Context context, boolean cancelable, OnCancelListener cancelListener) {
   super(context, cancelable, cancelListener);
   this.mContext = context;
 }

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   this.setContentView(R.layout.layout_sina_send_dialog);
   ib_dialog_sina_close = (ImageButton) findViewById(R.id.ib_dialog_sina_close);
   ll_dialog_sina_write = (LinearLayout) findViewById(R.id.ll_dialog_sina_write);
   ll_dialog_sina_time = (LinearLayout) findViewById(R.id.ll_dialog_sina_time);
   ll_dialog_sina_map = (LinearLayout) findViewById(R.id.ll_dialog_sina_map);
   ll_dialog_sina_menu = (LinearLayout) findViewById(R.id.ll_dialog_sina_menu);
   iv_dialog_sina_bg = (ImageView) findViewById(R.id.iv_dialog_sina_bg);
   iv_dialog_sina_des = (ImageView) findViewById(R.id.iv_dialog_sina_des);
   initView();
 }

private void initView() {
   setBrulBg();
   ll_dialog_sina_menu.setVisibility(View.VISIBLE);
   ll_dialog_sina_menu.setAnimation(AnimationUtil.moveToViewLocationFromTop());
   ib_dialog_sina_close.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       ll_dialog_sina_menu.setAnimation(AnimationUtil.moveToViewBottom());
       ll_dialog_sina_menu.setVisibility(View.GONE);
       dismiss();
     }
   });
   if(hideDes){
     iv_dialog_sina_des.setVisibility(View.GONE);
   }
 }

/**
  * 设置模糊背景
  */
 private void setBrulBg(){
   screenShot = CommonUtils.getInstance().getScreenShot((Activity) mContext);
   bitmap = CommonUtils.getInstance().zoomImg(screenShot, 0.2f);
   baos = new ByteArrayOutputStream();
   bitmap.compress(Bitmap.CompressFormat.JPEG, 1, baos);
   bytes = baos.toByteArray();
   Glide.with(mContext)
       .load(bytes)
       .asBitmap()
       .transform(new BlurTransformation(CommonUtils.getInstance().getContext(), 25))
       .into(iv_dialog_sina_bg);
 }

public void setClick(final SinaSendDialog mSinaSendDialog){
   this.show();

ll_dialog_sina_write.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       mSinaSendDialog.onNormalClick();
       dismiss();
     }
   });

ll_dialog_sina_map.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       mSinaSendDialog.onMapClick();
       dismiss();
     }
   });

ll_dialog_sina_time.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       mSinaSendDialog.onTimeClick();
       dismiss();
     }
   });

}

@Override
 public void dismiss() {
   super.dismiss();
   if(screenShot != null && !screenShot.isRecycled()){
     screenShot.recycle();
     screenShot = null;
   }
   if(bitmap != null && !bitmap.isRecycled()){
     bitmap.recycle();
     bitmap = null;
   }
   try {
     baos.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
   bytes = null;
   System.gc();
 }
}

布局文件


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
 >

<ImageView
   android:id="@+id/iv_dialog_sina_bg"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scaleType="fitXY"
   />
 <ImageButton
   android:id="@+id/ib_dialog_sina_close"
   android:layout_width="15dp"
   android:layout_height="15dp"
   android:src="@drawable/dialog_sina_send_close"
   android:background="@null"
   android:layout_gravity="bottom|center_horizontal"
   android:layout_marginBottom="17dp"
   />

<ImageView
   android:layout_width="match_parent"
   android:layout_height="0.5dp"
   android:background="@color/line_gray"
   android:layout_gravity="bottom"
   android:layout_marginBottom="50dp"
   />

<LinearLayout
   android:id="@+id/ll_dialog_sina_menu"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="bottom|center_horizontal"
   android:layout_marginBottom="120dp"
   android:orientation="horizontal"
   android:visibility="gone"
   >

<LinearLayout
     android:id="@+id/ll_dialog_sina_write"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:gravity="center_horizontal"
     >
     <ImageView
       android:layout_width="75dp"
       android:layout_height="75dp"
       android:src="@drawable/dialog_sina_send_write"
       />

<TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="13sp"
       android:textColor="@color/text_gray"
       android:text="一般内容"
       android:layout_marginTop="8dp"

/>
   </LinearLayout>
   <LinearLayout
     android:id="@+id/ll_dialog_sina_time"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:gravity="center_horizontal"
     android:layout_marginLeft="35dp"
     android:layout_marginRight="35dp"
     >
     <ImageView
       android:layout_width="75dp"
       android:layout_height="75dp"
       android:src="@drawable/dialog_sina_send_time"
       />

<TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="13sp"
       android:textColor="@color/text_gray"
       android:text="时间胶囊"
       android:layout_marginTop="8dp"

/>
   </LinearLayout>
   <LinearLayout
     android:id="@+id/ll_dialog_sina_map"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:gravity="center_horizontal"
     >
     <ImageView
       android:layout_width="75dp"
       android:layout_height="75dp"
       android:src="@drawable/dialog_sina_send_map"
       />

<TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="13sp"
       android:textColor="@color/text_gray"
       android:text="地点胶囊"
       android:layout_marginTop="8dp"

/>
   </LinearLayout>

</LinearLayout>

<ImageView
   android:id="@+id/iv_dialog_sina_des"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/dialog_sina_send_des"
   android:layout_gravity="center_horizontal"
   android:layout_marginTop="70dp"
   />

</FrameLayout>

Style


 <style name="SinaSendDialog">
   <item name="android:windowFullscreen">true</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
</style>

工具方法


 /**
  * 从控件的顶部移动到控件所在位置
  *
  * @return
  */
 public static TranslateAnimation moveToViewLocationFromTop() {
   TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
       -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
   mHiddenAction.setDuration(500);
   return mHiddenAction;
 }

/**
  * 截取当前屏幕
  * @param activity
  * @return
  */
 public Bitmap getScreenShot(Activity activity) {
   // 获取windows中最顶层的view
   View view = activity.getWindow().getDecorView();
   view.buildDrawingCache();

// 获取状态栏高度
   Rect rect = new Rect();
   view.getWindowVisibleDisplayFrame(rect);
   int statusBarHeights = rect.top;
   Display display = activity.getWindowManager().getDefaultDisplay();

// 获取屏幕宽和高
   int widths = display.getWidth();
   int heights = display.getHeight();

// 允许当前窗口保存缓存信息
   view.setDrawingCacheEnabled(true);

// 去掉状态栏
   Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,
       0, widths, heights);

// 销毁缓存信息
   view.destroyDrawingCache();

return bmp;
 }

/**
  * 改变bitmap宽高
  * @param bm
  * @param f
  * @return
  */
 public Bitmap zoomImg(Bitmap bm,float f){

int width = bm.getWidth();
   int height = bm.getHeight();

float scaleWidth = f;
   float scaleHeight = f;

Matrix matrix = new Matrix();
   matrix.postScale(scaleWidth, scaleHeight);

Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
   return newbm;
    }

接口


public interface SinaSendDialog {

void onNormalClick();

void onTimeClick();

void onMapClick();
}

基本讲一下逻辑,背景采用截屏高斯模糊处理,这里一定要降图片质量,不然会慢,按钮采用一个动画从上向下划出,虽然不是特别完美,但是多少有个样子。

源码地址:

https://github.com/bertsir/SinaSendView

到这里就结束啦.

来源:https://mp.weixin.qq.com/s/qr0iifLv94cX4sOWTp9tYg

标签:Android,新浪微博,菜单,界面
0
投稿

猜你喜欢

  • c# 接口interface基础入门小例子

    2023-11-04 16:55:45
  • SpringBoot Actuator未授权访问漏洞修复详解

    2022-03-30 16:43:28
  • Java的静态类型检查示例代码详解

    2023-08-09 10:42:13
  • java去除中文括号小括号,或者英文括号的实例代码

    2023-02-12 20:34:54
  • OpenGL实现Bezier曲线的方法示例

    2023-06-30 05:06:22
  • C#多态的三种实现方式(小结)

    2022-12-29 18:10:57
  • Android仿简书搜索框效果的示例代码

    2023-06-18 16:02:58
  • Java多线程:生产者与消费者案例

    2021-06-30 05:47:08
  • Android Kotlin使用SQLite案例详解

    2022-11-01 19:20:37
  • 详解Java中二叉树的基础概念(递归&迭代)

    2022-04-04 13:40:17
  • C#使用Clipboard类实现剪贴板功能

    2022-09-29 06:03:54
  • OpenCV实现反阈值二值化

    2022-06-29 16:02:33
  • Android编程开发之TextView单击链接弹出Activity的方法

    2023-08-06 18:27:11
  • Tornadofx学习笔记之IconTextFx开源库整合5000+个字体图标

    2023-06-07 12:18:04
  • El表达式使用问题javax.el.ELException:Failed to parse the expression的解决方式

    2023-11-24 12:47:13
  • Java String源码分析并介绍Sting 为什么不可变

    2021-09-23 06:10:42
  • Javaweb基础入门HTML之table与form

    2022-11-28 16:09:16
  • springboot配置文件中属性变量引用方式@@解读

    2023-11-24 20:39:18
  • android全局监控click事件的四种方式(小结)

    2023-05-02 07:33:31
  • Android点击事件之多点触摸与手势识别的实现

    2022-10-12 05:12:28
  • asp之家 软件编程 m.aspxhome.com