android模仿iphone气泡聊天 气泡大小自适应

这两天在做一个电子商城的商品评论功能,想到模仿微信或者iphone中的气泡聊天方式,气泡聊天是iphone内置的控件,不对开发者开放,android中更是没有提供类似的控件。于是先在百度google上搜了一下有没有类似的android实现方式,这样的帖子不多,也就局限于百度的第一页,以下面这个帖子为代表:http://blog.csdn.net/randyjiawenjie/article/details/6678738

大致的实现方法是使用“ListView+自定义adapter+气泡背景图片”,其他的帖子也是类似,而自己想实现的效果要求
气泡的大小能够“自适应聊天(短信)内容” ,固定的背景图片显然不能实现这个要求。再次寻寻觅觅,找到下面这个英文帖子:http://mobiforge.com/developing/story/sms-bubble-ui-iphone-apps ,大致讲解了在ios中模仿iphoe气泡的思路,其中关键是将气泡图片切割成9个栅格,如下图:


假设将图片的9个栅格区域按逆时针方向编号,中间 内容部分编号为9 ,使用ReleativeLayout+ImageView将9张图片依次逐行排列,注意图片切割时水平方向高度和垂直方向高度要一致,布局文件如下:

 

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

    <RelativeLayout
        android:id="@+id/bubble_lay_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     >
         <ImageView
            android:id="@+id/right_1"
            android:background="@drawable/right_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />
            
        
         <ImageView
            android:id="@+id/right_8"
            android:background="@drawable/right_8"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/right_1"
             />
          <ImageView
            android:id="@+id/right_7"
            android:background="@drawable/right_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/right_8"
             />
       
     </RelativeLayout>

     <RelativeLayout
        android:id="@+id/bubble_lay_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bubble_lay_1"
     >
         <ImageView
            android:id="@+id/right_2"
            android:background="@drawable/right_2"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
             />
            
        
         <TextView
            android:id="@+id/right_9"
            android:background="@drawable/right_9"
            android:textColor="#000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/right_2"
             />
          <ImageView
            android:id="@+id/right_6"
            android:background="@drawable/right_6"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_toRightOf="@id/right_9"
             />
       
     </RelativeLayout>
    
     <RelativeLayout
        android:id="@+id/bubble_lay_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bubble_lay_2"
     >
         <ImageView
            android:id="@+id/right_3"
            android:background="@drawable/right_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />
            
        
         <ImageView
            android:id="@+id/right_4"
            android:background="@drawable/right_4"
           
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/right_3"
             />
          <ImageView
            android:id="@+id/right_5"
            android:background="@drawable/right_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/right_4"
             />
       
     </RelativeLayout>
</RelativeLayout>

 



中间的TextView表示聊天内容部分,截取气泡图片9号栅格中的图片作为其背景,接着在代码中根据聊天内容调整2/6号,4/8号,9号的长宽,代码如下:

 

转载请标明来自:南通吃喜酒婚庆网

 

public class TestActivity extends Activity {
   
    private int textSize = 16;//聊天内容字体大小
    private String text ="heh你好呀he";//聊天内容
    private int bubbleMaxWidth = 100;//气泡的最大宽度

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        this.right();
    }
   
   
    private void right(){
        TextView t = (TextView)findViewById(R.id.right_9);//聊天内容TextView
        ImageView img2 = (ImageView)findViewById(R.id.right_2);
        ImageView img6 = (ImageView)findViewById(R.id.right_6);
        ImageView img4 = (ImageView)findViewById(R.id.right_4);
        ImageView img8 = (ImageView)findViewById(R.id.right_8);
       
        t.setTextSize(textSize);
        t.setText(text);
       
       //获得聊天内容在屏幕上的宽度,4/8号的宽度等于聊天内容的宽度
        TextPaint paint = t.getPaint();
        int width = (int)(paint.measureText(text));
       
        int height = textSize*2;//聊天内容只占一行的情况下,2/6/9号的宽度等于字号的两倍
   
        //如果聊天内容的宽度大于气泡的最大宽度,则让它换行
        if(width>=bubbleMaxWidth){
            height  = textSize*2*((int)(width/100)+1);//换行则调整2/6/9号的宽度
            width = bubbleMaxWidth;
            t.getLayoutParams().width=width;
        }
       
        img2.getLayoutParams().height=height;
        img6.getLayoutParams().height=height;
        t.getLayoutParams().height=height;

        img4.getLayoutParams().width=width;
        img8.getLayoutParams().width=width;
       
      
       
    }
 



剩下的则是,自定义adapter了,代码如下:

public class ShareCommentAdapter extends BaseAdapter {

    private Activity context;
    private List<ShareComment> commentList;
    private int textSize = 16;
    private int bubbleMaxWidth;
   
    public ShareCommentAdapter (Activity context,List<ShareComment> commentList){
            Display display = context.getWindowManager().getDefaultDisplay();
            bubbleMaxWidth = (int)(display.getWidth()*0.6);
            this.context = context; 
            this.commentList = commentList; 
            } 
   
    @Override
    public int getCount() {
        return commentList.size();
    }

    @Override
    public Object getItem(int position) {
        return commentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
       
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = null;
        ShareComment  comment = commentList.get(position);
       
        if(comment.getIsHost()==1){ 
            view = mInflater.inflate(R.layout.share_comment_right_item, null); 
            this.setRightBubbleStyle(view, comment.getComment());
        }else{
            view = mInflater.inflate(R.layout.share_comment_left_item, null);
            this.setLeftBubbleStyle(view, comment.getComment());
        }
       
        //ViewGroup.LayoutParams params = new LayoutParams(100,100);
        //view.setLayoutParams(params);
        return view;
    }
 



其中 setLeftBubbleStyle(view, comment.getComment()) 就是上上面设置气泡样式的right()方法的内容,最终效果如下:

android气泡聊天(sms)

感兴趣的可以去我的网站下载源代码,源代码包含左右两个方向 的气泡,其中左边的已经注释掉了,下载地址源码

 

 

android 随意漂浮动画,Android实现气泡漂浮动画,类似IOS Game Center中气泡动画 网上没找到,于是自己写了一个,简单的算法,利用 PropertyValuesHolder实现多个动画的集合,不多说,直接上代码吧,非常好理解:/***气泡漂浮动画*@paramview*@paramduration动画运行时间*@paramoffset动画运行幅度*@paramrepeatCount动画运行次数*@return*/publicstaticO... 阅读详情

相关推荐

【信息科学与工程学】计算机科学与自动化——第六十三篇 人机交互之前端交互参数知识库01

此方案可满足千万级并发需求,同时支持时序数据、知识图谱和文档数据库的高效处理,通过云原生架构实现极致弹性。​:每月约$15-20万(100节点K8s集群+数据库集群+CDN流量)1. 宠物上架/下架流程。

weixin_49199313的博客 1418

Android仿iphone-气泡短信-DEMO.zip

Android仿iphone-气泡短信-DEMO.zip

Android气泡短信界面仿实现DEMO

Android权限系统是Android安全模型的核心部分,它确保应用在执行某些操作之前获得了用户的同意。以下是一些Android权限管理的关键点:权限声明:在应用的文件中声明应用需要的权限,这是在安装应用之前系统需要检查的内容。权限请求:在应用运行时,如果需要执行受保护的操作(如访问联系人、发送短信等),则需要向用户请求相应的权限。这通常是通过弹出对话框的形式来进行。权限授予与撤销:用户可以授予或撤销应用的权限。权限的授予可以在安装时进行,也可以在应用运行时进行。应用可以使用。

weixin_28850145的博客 729

Android应用源码之仿iphone 气泡短信-IT计算机-毕业设计.zip

Android应用源码开发Demo,主要用于毕业设计学习。

Android仿iOS Messages聊天气泡

Android仿iOS Messages聊天气泡一、目标二、功能分析三、实现代码1. ChatItem2. DateItem3. TextItem4. PhotoItem5. ChatViewHolder四、开发过程回顾五、接下来六、Finally 在《iOS Messages显示图片功能分析》一文中,我们总结了iOS Messages的气泡形状。现在着手实现之。 一、目标 实现iOS Mess...

济沧海 x 远沧溟 1175

ios 仿微信短信聊天气泡

苹果短信聊天气泡微信聊天气泡一直很经典,很小的一个气泡根据文字的多少适当变大变小。 其实实现很简单,主要是控件的自适应撑高,这里用到的是cell。 核心代码 - (UIView*)bubbleView:(NSString *)textimageName:(NSString *)name { UIView *returnView= [[UIViewalloc] ini...

maozhuxigood 542

聊天气泡效果实现

仿iphone 气泡短信 http://www.apkbus.com/android-110117-1-1.html 仿iphone 气泡短信 DEMO http://www.eoeandroid.com/thread-112801-1-1.html android模仿iphone气泡聊天 气泡大小自适应 http://enzoqin.iteye.co

2600

Aurora IMUI消息气泡自定义终极指南:打造个性化聊天视觉效果

想要为你的聊天应用设计独特的消息气泡效果吗?Aurora IMUI作为一款功能强大的跨平台IM UI组件库,提供了灵活的消息气泡自定义功能,让开发者能够轻松实现个性化的聊天视觉效果。😊 ## 什么是Aurora IMUI消息气泡? Aurora IMUI是一个跨平台的即时通讯UI组件库,支持AndroidiOS和ReactNative平台。其中**消息气泡**是聊天界面中最核心的视觉元素,

gitblog_00639的博客 427

实现自适应内容宽度和高度的聊天气泡背景

在现代用户界面设计中,自适应背景设计已成为提升用户交互体验的重要组成部分。气泡背景自适应指的是当用户界面的元素(如按钮或标签)大小发生变化时,其背景图像能够相应地自动调整大小以填满内容区域。这种方式不仅增强了视觉效果,还提高了用户界面的响应性和适应性。本章将探讨气泡背景自适应的基本概念,介绍其在不同前端技术(如CSS和JavaScript)中的应用方法,并通过实际案例分析如何优化设计以实现更佳的用户体验。

weixin_42581846的博客 832

android 聊天气泡背景图片,聊天气泡背景图片拉伸设置

以前一直对类似聊天气泡背景图片拉伸的设置纠结,不管如何设置UIEdgeInsets属性都不能正常设置,今天对以下几种情况进行了总结,如有需要的同学可进行参考:说明:/**1、UIEdgeInsets中的值都可以以像素为单位进行设置2、测量凸起部分距离图片边缘值时,若有存在圆角部分,计算时最好将圆角部分出去,如: - (void)addDialogueLeft;方法中左侧凸起部分为12像素,图片额外...

weixin_39634985的博客 622

android仿iPhone气泡消息样式

NULL 博文链接:https://xiaoliefengfeng.iteye.com/blog/1159673

仿iphone 气泡短信 DEMO

仿iphone 气泡短信 DEMO

仿iphone气泡短信 DEMO

仿iphone 气泡效果 短信 自定义的LIST

UI之高仿iphone短信发送气泡效果

UI之高仿iphone短信发送气泡效果,已经测试,请放心使用,在android2.3下即可运行

android气泡聊天大小,android – 备用聊天气泡宽度

我正在开发一个聊天类型的应用程序我正在使用两个不同的九个补丁来聊天泡泡主要消息和响应.我面临的问题是根据消息长度自动包装气泡的宽度.以下是我的主要布局:android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1">and...

weixin_36429576的博客 203

android仿QQ长按气泡弹窗效果,仿IOS控件,支持跟随手指显示

参考: BubblePopupWindow:https://github.com/smuyyh/BubblePopupWindow PopupMenuView:https://github.com/kareluo/PopupMenuView Android仿QQ长按删除弹出框:https://blog.csdn.net/shangmingchao/article/details/50244161#...

皮小智的博客 2687

Android qq消息气泡实现效果,Android 实现仿QQ拖拽气泡效果的示例

效果图:一、实现思路在列表中默认使用自定义的TextView控件来展示消息气泡,在自定义的TextView控件中重写onTouchEvent方法,然后在DOWN、MOVE、UP事件中分别处理拖拽效果。整个拖拽效果我们可以拆分成以下几步来实现:1.默认状态2.两气泡相连状态3.两气泡分离状态4.气泡消失状态二、功能实现默认状态:用来做一个状态的标识,无需特别处理。两气泡相连状态:绘制一个固定圆和一个...

weixin_36462845的博客 644

android handcent短信 仿iphone气泡聊天

废话不说直接上图

java_wd的专栏 1467
iteye_16107
博客等级 码龄8年 0粉丝 1原创
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值