你難道還不清楚Nine-Patch

1. 什么是Nine-Patch圖片

它是一種被特殊處理過的png圖片,能夠指定哪些區域可以被拉伸、哪些區域不可以。如果聊天的信息背景圖片不用Nine-Patch圖片,那么當你一條信息量很大的時候,整個圖片會被往長的拉伸往寬的拉伸。而我們需要的只是圖片的某一些部分被拉伸。

  • 制作Nine-Patch圖片
  1. 配置JDK的bin目錄配置到環境變量當中。
  2. 在Android sdk目錄下有一個tools文件夾,在這個文件夾中找到draw9patch.bat文件,我們就是使用它來制作Nine-Patch圖片的。
  3. 雙擊draw9patch.bat文件,在導航欄點擊File-Open 9-patch將你想制作成Nine-Patch的圖片加載進來,圖片哪里需要拉伸,就在圖片的邊緣拖動進行繪制即可,按住Shift鍵拖動可以進行擦除。
  4. 最后點擊導航欄File-Save9-patch把繪制好的圖片進行保存,此時文件名就是xxx.9.png圖片。

2. 編寫精美的聊天界面

需求:精美的聊天界面。
分析:上面的部分是RecyclerView,下面的部分是EditText+Button
方法:編寫Recycler,處理Button點擊事件,把信息放到Recyc中。

2.1 添加Recyc的依賴:

compile 'com.android.support:recyclerview-v7:25.0.1'

2.2 編寫mainactivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.fkq.mytask.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Type something here"
            android:maxLines="2" />

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send" />
    </LinearLayout>
</LinearLayout>

說明:上面是一個Recyc,下面是Edit+Button組合。
2.3 編寫Recyc:

  • 新建消息的實體類Msg:
public class Msg {
    public static final int TYPE_RECEIVED = 0;
    public static final int TYPE_SENT = 1;
    private String content;
    private int type;

    public Msg(String content, int type) {
        this.content = content;
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public int getType() {
        return type;
    }

}

說明:Msg類有兩個字段,一個是內容,一個是內容的類型,其中內容的類型包括兩類:TYPE_RECEIVED代表收到的信息;TYPE_SENT代表發出的信息。
2.4 新建msg_item.xml:

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

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:background="@drawable/message_left">

        <TextView
            android:id="@+id/left_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#fff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/message_right">

        <TextView
            android:id="@+id/right_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp" />
    </LinearLayout>

</LinearLayout>

說明:讓收到的信息居左對齊,讓發出的信息居右對齊,并且分別使用message_left和message_right作為背景圖。

  • 新建適配器MsgAdapter:
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Msg> mMsgList;

    public MsgAdapter(List<Msg> MsgList) {
        this.mMsgList = MsgList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftLayout;
        LinearLayout rightLayout;

        TextView leftMsg;
        TextView rightMsg;

        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = (LinearLayout) itemView.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) itemView.findViewById(R.id.right_layout);
            leftMsg = (TextView) itemView.findViewById(R.id.left_msg);
            rightMsg = (TextView) itemView.findViewById(R.id.right_msg);
        }
    }

    @Override
    public MsgAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MsgAdapter.ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType()== Msg.TYPE_RECEIVED){
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        }else if (msg.getType()==Msg.TYPE_SENT){
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}

說明:在onBindViewHolder方法中增加了對消息類型的判斷,如果這條信息是收到的,則顯示左邊的消息布局,如果這條消息是發出的,則顯示右邊的消息布局。

2.5 生成RecyclerView,處理Button的點擊事件

public class MainActivity extends AppCompatActivity {
    private List<Msg> msgList = new ArrayList<>();
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
    private EditText inputtext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initMsgs();
        inputtext = (EditText) findViewById(R.id.et_text);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView) findViewById(R.id.rv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputtext.getText().toString();
                if (!"".equals(content)) {
                    Msg msg = new Msg(content, Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size() - 1);
                    msgRecyclerView.scrollToPosition(msgList.size() - 1);
                    inputtext.setText("");
                }
            }
        });

    }

    private void initMsgs() {
        Msg msg1 = new Msg("Hello baby", Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Who are you?", Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom.", Msg.TYPE_RECEIVED);
        msgList.add(msg3);

    }
}

說明:

  1. initMsgs方法初始化幾條數據顯示在RecyclerView中顯示。
  2. 點擊Buttton按鈕獲取EditText內容,如果內容不為null,則創建出一個新的Msg對象,并把它添加到msgList列表中去。
  3. 調用了適配器的notifyItemInserted方法,用于通知列表有新的數據插入,這樣新增的一條信息才能夠在RecyclerView中顯示。
  4. 調用Recyc的scrollToPosition將顯示的數據定位到最后一行,以保證一定可以看到最后發出的一條信息。
  5. 調用setText方法將輸入的內容清空。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容