Commit 55e73a5e02709b14c2faa78d5c9019fb5c056374
1 parent
61429a1f
更新逻辑
Showing
11 changed files
with
512 additions
and
77 deletions
app/build.gradle
app/libs/libcnliveutil.jar
No preview for this file type
app/src/main/java/com/cnlive/im/CNLiveApplication.java
| ... | ... | @@ -14,7 +14,7 @@ public class CNLiveApplication extends Application { |
| 14 | 14 | @Override |
| 15 | 15 | public void onCreate() { |
| 16 | 16 | super.onCreate(); |
| 17 | - Config.init(this, "60_j07o9s9b65", "dc1e76f87c7c2d83dcedb8a1204ec22fca3e156b21eb0d"); | |
| 17 | + Config.init(this, "60_j07o9s9b65", "dc1e76f87c7c2d83dcedb8a1204ec22fca3e156b21eb0d",true); | |
| 18 | 18 | } |
| 19 | 19 | } |
| 20 | 20 | ... | ... |
app/src/main/java/com/cnlive/im/adapter/CommentAdapter.java
| ... | ... | @@ -8,12 +8,14 @@ import android.view.ViewGroup; |
| 8 | 8 | import android.widget.ImageView; |
| 9 | 9 | import android.widget.TextView; |
| 10 | 10 | |
| 11 | +import com.bumptech.glide.Glide; | |
| 11 | 12 | import com.cnlive.im.R; |
| 12 | 13 | import com.cnlive.libs.util.model.CommentItem; |
| 13 | 14 | |
| 14 | 15 | import java.util.ArrayList; |
| 15 | 16 | import java.util.List; |
| 16 | 17 | |
| 18 | + | |
| 17 | 19 | /** |
| 18 | 20 | * @author 陈硕 |
| 19 | 21 | * @time 2017/6/9 10:56 |
| ... | ... | @@ -60,6 +62,7 @@ public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentH |
| 60 | 62 | public void onBindViewHolder(CommentHolder holder, int position) { |
| 61 | 63 | |
| 62 | 64 | final CommentItem commentItem = mItems.get(position); |
| 65 | + Glide.with(context).load(commentItem.getFacepath()).into(commentHolder.img_head); | |
| 63 | 66 | commentHolder.nick_name.setText(commentItem.getUid()); |
| 64 | 67 | commentHolder.comment_message.setText(commentItem.getContent()); |
| 65 | 68 | commentHolder.comment_good_count.setText(commentItem.getPraise()); |
| ... | ... | @@ -84,7 +87,7 @@ public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentH |
| 84 | 87 | this.onImageViewClick = onImageViewClick; |
| 85 | 88 | } |
| 86 | 89 | |
| 87 | - public interface OnImageViewClick { | |
| 90 | + public interface OnImageViewClick { | |
| 88 | 91 | void praiseComment(CommentItem commentItem); |
| 89 | 92 | } |
| 90 | 93 | |
| ... | ... | @@ -94,6 +97,7 @@ public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentH |
| 94 | 97 | public final TextView comment_message; |
| 95 | 98 | public final TextView comment_good_count; |
| 96 | 99 | public final ImageView good_imv1; |
| 100 | + public final ImageView img_head; | |
| 97 | 101 | |
| 98 | 102 | public CommentHolder(View itemView) { |
| 99 | 103 | super(itemView); |
| ... | ... | @@ -101,6 +105,7 @@ public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentH |
| 101 | 105 | good_imv1 = (ImageView) itemView.findViewById(R.id.good_imv); |
| 102 | 106 | comment_message = (TextView) itemView.findViewById(R.id.comment_message); |
| 103 | 107 | comment_good_count = (TextView) itemView.findViewById(R.id.comment_good_count); |
| 108 | + img_head = (ImageView) itemView.findViewById(R.id.img_head); | |
| 104 | 109 | |
| 105 | 110 | |
| 106 | 111 | } | ... | ... |
app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
| ... | ... | @@ -73,8 +73,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 73 | 73 | user_name_string = intent.getStringExtra("user_name_string"); |
| 74 | 74 | String user_id_string = intent.getStringExtra("user_id_string"); |
| 75 | 75 | if (user_name_string != null) |
| 76 | - ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, "")); | |
| 77 | - else ChatUtil.logOut(); | |
| 76 | + ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener); | |
| 77 | + else ChatUtil.logOut(connectListener); | |
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
| ... | ... | @@ -103,15 +103,16 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 103 | 103 | /** |
| 104 | 104 | * 发送消息监听 |
| 105 | 105 | */ |
| 106 | - private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { | |
| 107 | 106 | |
| 107 | + private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { | |
| 108 | 108 | @Override |
| 109 | - public void onAttached(CNMessage cnMessage) { | |
| 110 | - Log.v(TAG, "SendMessageListener onAttached" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName()); | |
| 109 | + public void onAttached(Object o) { | |
| 110 | + | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | @Override |
| 114 | - public void onSuccess(final CNMessage cnMessage) { | |
| 114 | + public void onSuccess(Object o) throws JSONException { | |
| 115 | + final CNMessage cnMessage = (CNMessage) o; | |
| 115 | 116 | mHandler.post(new Runnable() { |
| 116 | 117 | @Override |
| 117 | 118 | public void run() { |
| ... | ... | @@ -129,21 +130,55 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 129 | 130 | |
| 130 | 131 | } |
| 131 | 132 | }); |
| 132 | - | |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | @Override |
| 136 | - public void onError(CNMessage cnMessage, int i, final String s) { | |
| 137 | - Log.v(TAG, "SendMessageListener 失败 " + i + " s" + s); | |
| 138 | - mHandler.post(new Runnable() { | |
| 139 | - @Override | |
| 140 | - public void run() { | |
| 141 | - Toast.makeText(ChatRoomActivity.this, s, Toast.LENGTH_SHORT).show(); | |
| 142 | - } | |
| 143 | - }); | |
| 136 | + public void onError(Object o, int i, String s) { | |
| 137 | + | |
| 144 | 138 | } |
| 145 | 139 | }; |
| 146 | 140 | |
| 141 | + /* private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { | |
| 142 | + | |
| 143 | + @Override | |
| 144 | + public void onAttached(CNMessage cnMessage) { | |
| 145 | + Log.v(TAG, "SendMessageListener onAttached" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName()); | |
| 146 | + } | |
| 147 | + | |
| 148 | + @Override | |
| 149 | + public void onSuccess(final CNMessage cnMessage) { | |
| 150 | + mHandler.post(new Runnable() { | |
| 151 | + @Override | |
| 152 | + public void run() { | |
| 153 | + try { | |
| 154 | + if (((String) new JSONObject(cnMessage.getContent()).get("type")).equals("2")) | |
| 155 | + return; | |
| 156 | + } catch (JSONException e) { | |
| 157 | + e.printStackTrace(); | |
| 158 | + } | |
| 159 | + if ((roomIdOne).equals(cnMessage.getTargetId())) { | |
| 160 | + groupChatOneListMy.add(new CNMessageInfo(cnMessage, true)); | |
| 161 | + chatListAdapterOne.updateItems(groupChatOneListMy); | |
| 162 | + chatListAdapterOne.notifyDataSetChanged(); | |
| 163 | + } | |
| 164 | + | |
| 165 | + } | |
| 166 | + }); | |
| 167 | + | |
| 168 | + } | |
| 169 | + | |
| 170 | + @Override | |
| 171 | + public void onError(CNMessage cnMessage, int i, final String s) { | |
| 172 | + Log.v(TAG, "SendMessageListener 失败 " + i + " s" + s); | |
| 173 | + mHandler.post(new Runnable() { | |
| 174 | + @Override | |
| 175 | + public void run() { | |
| 176 | + Toast.makeText(ChatRoomActivity.this, s, Toast.LENGTH_SHORT).show(); | |
| 177 | + } | |
| 178 | + }); | |
| 179 | + } | |
| 180 | + }; | |
| 181 | + */ | |
| 147 | 182 | //连接状态监听 |
| 148 | 183 | private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() { |
| 149 | 184 | @Override |
| ... | ... | @@ -152,16 +187,15 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 152 | 187 | } |
| 153 | 188 | }; |
| 154 | 189 | |
| 155 | - //接收消息监听 | |
| 190 | + | |
| 156 | 191 | private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { |
| 157 | 192 | @Override |
| 158 | - public void processMessage(final CNMessage cnMessage) { | |
| 159 | - Log.v(TAG, "receiveMessageListener" + " " + cnMessage.getTargetId() + " " + cnMessage.getContent()); | |
| 193 | + public void processMessage(final Object o) { | |
| 194 | + final CNMessage cnMessage = (CNMessage) o; | |
| 160 | 195 | |
| 161 | 196 | mHandler.post(new Runnable() { |
| 162 | 197 | @Override |
| 163 | 198 | public void run() { |
| 164 | - | |
| 165 | 199 | try { |
| 166 | 200 | JSONObject jsonObject = new JSONObject(cnMessage.getContent().toString()); |
| 167 | 201 | String type = (String) jsonObject.get("type"); |
| ... | ... | @@ -183,12 +217,36 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 183 | 217 | }; |
| 184 | 218 | |
| 185 | 219 | |
| 220 | + //接收消息监听 | |
| 221 | + /*private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { | |
| 222 | + @Override | |
| 223 | + public void processMessage(final CNMessage cnMessage) { | |
| 224 | + Log.v(TAG, "receiveMessageListener" + " " + cnMessage.getTargetId() + " " + cnMessage.getContent()); | |
| 186 | 225 | |
| 226 | + mHandler.post(new Runnable() { | |
| 227 | + @Override | |
| 228 | + public void run() { | |
| 187 | 229 | |
| 188 | - | |
| 189 | - | |
| 190 | - | |
| 191 | - | |
| 230 | + try { | |
| 231 | + JSONObject jsonObject = new JSONObject(cnMessage.getContent().toString()); | |
| 232 | + String type = (String) jsonObject.get("type"); | |
| 233 | + if ("2".equals(type)) { | |
| 234 | + Toast.makeText(ChatRoomActivity.this, cnMessage.getUserInfo().getUserName() + "加入聊天室", Toast.LENGTH_SHORT).show(); | |
| 235 | + return; | |
| 236 | + } | |
| 237 | + } catch (JSONException e) { | |
| 238 | + e.printStackTrace(); | |
| 239 | + } | |
| 240 | + { | |
| 241 | + groupChatOneListMy.add(new CNMessageInfo(cnMessage, false)); | |
| 242 | + chatListAdapterOne.updateItems(groupChatOneListMy); | |
| 243 | + chatListAdapterOne.notifyDataSetChanged(); | |
| 244 | + } | |
| 245 | + } | |
| 246 | + }); | |
| 247 | + } | |
| 248 | + }; | |
| 249 | +*/ | |
| 192 | 250 | |
| 193 | 251 | |
| 194 | 252 | //聊天室操作 |
| ... | ... | @@ -332,7 +390,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 332 | 390 | Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s); |
| 333 | 391 | |
| 334 | 392 | |
| 335 | - | |
| 336 | 393 | } |
| 337 | 394 | }; |
| 338 | 395 | ... | ... |
app/src/main/java/com/cnlive/im/ui/CommentActivity.java
| ... | ... | @@ -97,7 +97,7 @@ public class CommentActivity extends AppCompatActivity implements View.OnClickLi |
| 97 | 97 | @Override |
| 98 | 98 | public void onResponse(CommentPage commentPage) { |
| 99 | 99 | if (commentPage.getData() != null) { |
| 100 | -Log.v("commentPage",commentPage.getErrorMessage()+""); | |
| 100 | + Log.v("commentPage", commentPage.getErrorMessage() + ""); | |
| 101 | 101 | List<CommentItem> comments = commentPage.getData().getComments(); |
| 102 | 102 | |
| 103 | 103 | recyclerView.setAdapter(commentAdapter); |
| ... | ... | @@ -113,7 +113,7 @@ Log.v("commentPage",commentPage.getErrorMessage()+""); |
| 113 | 113 | commentUtil.programComment(programID, new CommentProgramCallback() { |
| 114 | 114 | @Override |
| 115 | 115 | public void onResponse(CommentProgram commentProgram) { |
| 116 | - Log.v("commentProgram",commentProgram.getData().getComments().size()+""); | |
| 116 | + Log.v("commentProgram", commentProgram.getData().getComments().size() + ""); | |
| 117 | 117 | |
| 118 | 118 | } |
| 119 | 119 | }); | ... | ... |
app/src/main/java/com/cnlive/im/ui/MainActivity.java
| ... | ... | @@ -50,6 +50,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe |
| 50 | 50 | public void onSuccess(String s) { |
| 51 | 51 | Intent intent = new Intent(MainActivity.this,LoGinActivity.class); |
| 52 | 52 | startActivity(intent); |
| 53 | + ChatUtil.removeConnectListener(connectListener); | |
| 53 | 54 | |
| 54 | 55 | } |
| 55 | 56 | ... | ... |
app/src/main/java/com/cnlive/im/ui/PrivateChatActivity.java
| ... | ... | @@ -139,7 +139,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 139 | 139 | edit_layout.setVisibility(View.VISIBLE); |
| 140 | 140 | chatListView.setVisibility(View.VISIBLE); |
| 141 | 141 | CNUserInfo cnName = new CNUserInfo(user_id_string, user_name_string, ""); |
| 142 | - ChatUtil.login(cnName); | |
| 142 | + ChatUtil.login(cnName,connectListener); | |
| 143 | 143 | |
| 144 | 144 | } |
| 145 | 145 | |
| ... | ... | @@ -195,7 +195,58 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 195 | 195 | /** |
| 196 | 196 | * 发送消息监听 |
| 197 | 197 | */ |
| 198 | - private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { | |
| 198 | + | |
| 199 | + private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { | |
| 200 | + @Override | |
| 201 | + public void onAttached(Object o) { | |
| 202 | + | |
| 203 | + } | |
| 204 | + | |
| 205 | + @Override | |
| 206 | + public void onSuccess(Object o) throws JSONException { | |
| 207 | + final CNMessage cnMessage = (CNMessage) o; | |
| 208 | + | |
| 209 | + if (View.VISIBLE == vistor_btn.getVisibility()) { | |
| 210 | + vistor_btn.setVisibility(View.GONE); | |
| 211 | + } | |
| 212 | + mHandler.post(new Runnable() { | |
| 213 | + @Override | |
| 214 | + public void run() { | |
| 215 | + JSONObject jsonObject = null; | |
| 216 | + try { | |
| 217 | + jsonObject = new JSONObject(cnMessage.getContent().toString()); | |
| 218 | + String type = (String) jsonObject.get("type"); | |
| 219 | + String message = (String) jsonObject.get("message"); | |
| 220 | + Toast.makeText(PrivateChatActivity.this, "发送消息成功", Toast.LENGTH_SHORT).show(); | |
| 221 | + if ("3".equals(type) && message.equals("你好我们可以进行私聊吗")) { | |
| 222 | + sendInfo = 2; | |
| 223 | + Toast.makeText(PrivateChatActivity.this, "申请已发送等待对方验证", Toast.LENGTH_SHORT).show(); | |
| 224 | + } else if ("3".equals(type) && message.equals("拒绝")) { | |
| 225 | + sendInfo=5; | |
| 226 | + } else if ("3".equals(type) && message.equals("同意")) { | |
| 227 | + sendInfo=1; | |
| 228 | + } else if ("4".equals(type)) { | |
| 229 | + messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage, true)); | |
| 230 | + chatListAdapter.updateItems(messageList); | |
| 231 | + chatListAdapter.notifyDataSetChanged(); | |
| 232 | + } | |
| 233 | + } catch (JSONException e) { | |
| 234 | + | |
| 235 | + | |
| 236 | + } | |
| 237 | + | |
| 238 | + } | |
| 239 | + }); | |
| 240 | + } | |
| 241 | + | |
| 242 | + @Override | |
| 243 | + public void onError(Object o, int i, String s) { | |
| 244 | + | |
| 245 | + } | |
| 246 | + }; | |
| 247 | + | |
| 248 | + | |
| 249 | + /*private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { | |
| 199 | 250 | |
| 200 | 251 | @Override |
| 201 | 252 | public void onAttached(CNMessage cnMessage) { |
| ... | ... | @@ -256,7 +307,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 256 | 307 | }); |
| 257 | 308 | |
| 258 | 309 | } |
| 259 | - }; | |
| 310 | + };*/ | |
| 260 | 311 | |
| 261 | 312 | //连接状态监听 |
| 262 | 313 | private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() { |
| ... | ... | @@ -268,7 +319,45 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 268 | 319 | } |
| 269 | 320 | }; |
| 270 | 321 | //接收消息监听 |
| 271 | - private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { | |
| 322 | + private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { | |
| 323 | + @Override | |
| 324 | + public void processMessage(Object o) { | |
| 325 | + final CNMessage cnMessage = (CNMessage) o; | |
| 326 | + mHandler.post(new Runnable() { | |
| 327 | + @Override | |
| 328 | + public void run() { | |
| 329 | + Log.v("cnMessage", cnMessage.getTargetId() + "," + cnMessage.getUserInfo().getUserId()); | |
| 330 | + JSONObject jsonObject = null; | |
| 331 | + try { | |
| 332 | + jsonObject = new JSONObject(cnMessage.getContent().toString()); | |
| 333 | + String type = (String) jsonObject.get("type"); | |
| 334 | + if ("3".equals(type) && "你好我们可以进行私聊吗".equals(jsonObject.get("message"))) { | |
| 335 | + vistor_btn.setVisibility(View.GONE); | |
| 336 | + agree_reject_lin.setVisibility(View.VISIBLE); | |
| 337 | + } else if ("3".equals(type) && "同意".equals(jsonObject.get("message"))) { | |
| 338 | + sendInfo = 1; | |
| 339 | + } else if ("3".equals(type) && "拒绝".equals(jsonObject.get("message"))) { | |
| 340 | + sendInfo = 4; | |
| 341 | + } else if ("4".equals(type)) { | |
| 342 | + Log.v(TAG, cnMessage.getContent().toString()); | |
| 343 | + messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage, false)); | |
| 344 | + chatListAdapter.updateItems(messageList); | |
| 345 | + chatListAdapter.notifyDataSetChanged(); | |
| 346 | + } | |
| 347 | + | |
| 348 | + } catch (JSONException e) { | |
| 349 | + e.printStackTrace(); | |
| 350 | + } | |
| 351 | + | |
| 352 | + | |
| 353 | + } | |
| 354 | + }); | |
| 355 | + | |
| 356 | + } | |
| 357 | + }; | |
| 358 | + | |
| 359 | + | |
| 360 | + /* private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { | |
| 272 | 361 | @Override |
| 273 | 362 | public void processMessage(final CNMessage cnMessage) { |
| 274 | 363 | |
| ... | ... | @@ -304,7 +393,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 304 | 393 | }); |
| 305 | 394 | |
| 306 | 395 | } |
| 307 | - }; | |
| 396 | + };*/ | |
| 308 | 397 | |
| 309 | 398 | |
| 310 | 399 | } | ... | ... |
app/src/main/java/com/cnlive/im/view/CircleImageView.java
0 → 100644
| 1 | +package com.cnlive.im.view; | |
| 2 | + | |
| 3 | +import android.annotation.SuppressLint; | |
| 4 | +import android.content.Context; | |
| 5 | +import android.content.res.TypedArray; | |
| 6 | +import android.graphics.Bitmap; | |
| 7 | +import android.graphics.BitmapShader; | |
| 8 | +import android.graphics.Canvas; | |
| 9 | +import android.graphics.Color; | |
| 10 | +import android.graphics.Matrix; | |
| 11 | +import android.graphics.Paint; | |
| 12 | +import android.graphics.RectF; | |
| 13 | +import android.graphics.Shader; | |
| 14 | +import android.graphics.drawable.BitmapDrawable; | |
| 15 | +import android.graphics.drawable.ColorDrawable; | |
| 16 | +import android.graphics.drawable.Drawable; | |
| 17 | +import android.util.AttributeSet; | |
| 18 | +import android.widget.ImageView; | |
| 19 | + | |
| 20 | +import com.cnlive.im.R; | |
| 21 | + | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * @author 陈硕 | |
| 25 | + * @time 2016/11/7 10:27 | |
| 26 | + * @desc ${TODD} | |
| 27 | + */ | |
| 28 | +@SuppressLint("AppCompatCustomView") | |
| 29 | +public class CircleImageView extends ImageView { | |
| 30 | + | |
| 31 | + | |
| 32 | + private static final ImageView.ScaleType SCALE_TYPE = ImageView.ScaleType.CENTER_CROP; | |
| 33 | + | |
| 34 | + private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888; | |
| 35 | + private static final int COLORDRAWABLE_DIMENSION = 1; | |
| 36 | + | |
| 37 | + private static final int DEFAULT_BORDER_WIDTH = 0; | |
| 38 | + private static final int DEFAULT_BORDER_COLOR = Color.BLACK; | |
| 39 | + | |
| 40 | + private final RectF mDrawableRect = new RectF(); | |
| 41 | + private final RectF mBorderRect = new RectF(); | |
| 42 | + | |
| 43 | + private final Matrix mShaderMatrix = new Matrix(); | |
| 44 | + private final Paint mBitmapPaint = new Paint(); | |
| 45 | + private final Paint mBorderPaint = new Paint(); | |
| 46 | + | |
| 47 | + private int mBorderColor = DEFAULT_BORDER_COLOR; | |
| 48 | + private int mBorderWidth = DEFAULT_BORDER_WIDTH; | |
| 49 | + | |
| 50 | + private Bitmap mBitmap; | |
| 51 | + private BitmapShader mBitmapShader; | |
| 52 | + private int mBitmapWidth; | |
| 53 | + private int mBitmapHeight; | |
| 54 | + | |
| 55 | + private float mDrawableRadius; | |
| 56 | + private float mBorderRadius; | |
| 57 | + | |
| 58 | + private boolean mReady; | |
| 59 | + private boolean mSetupPending; | |
| 60 | + | |
| 61 | + public CircleImageView(Context context) { | |
| 62 | + super(context); | |
| 63 | + } | |
| 64 | + | |
| 65 | + public CircleImageView(Context context, AttributeSet attrs) { | |
| 66 | + this(context, attrs, 0); | |
| 67 | + } | |
| 68 | + | |
| 69 | + public CircleImageView(Context context, AttributeSet attrs, int defStyle) { | |
| 70 | + super(context, attrs, defStyle); | |
| 71 | + super.setScaleType(SCALE_TYPE); | |
| 72 | + | |
| 73 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0); | |
| 74 | + | |
| 75 | + mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH); | |
| 76 | + mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR); | |
| 77 | + | |
| 78 | + a.recycle(); | |
| 79 | + | |
| 80 | + mReady = true; | |
| 81 | + | |
| 82 | + if (mSetupPending) { | |
| 83 | + setup(); | |
| 84 | + mSetupPending = false; | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | + @Override | |
| 89 | + public ScaleType getScaleType() { | |
| 90 | + return SCALE_TYPE; | |
| 91 | + } | |
| 92 | + | |
| 93 | + @Override | |
| 94 | + public void setScaleType(ScaleType scaleType) { | |
| 95 | + if (scaleType != SCALE_TYPE) { | |
| 96 | + throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType)); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + @Override | |
| 101 | + protected void onDraw(Canvas canvas) { | |
| 102 | + if (getDrawable() == null) { | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + | |
| 106 | + canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint); | |
| 107 | + canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Override | |
| 111 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
| 112 | + super.onSizeChanged(w, h, oldw, oldh); | |
| 113 | + setup(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + public int getBorderColor() { | |
| 117 | + return mBorderColor; | |
| 118 | + } | |
| 119 | + | |
| 120 | + public void setBorderColor(int borderColor) { | |
| 121 | + if (borderColor == mBorderColor) { | |
| 122 | + return; | |
| 123 | + } | |
| 124 | + | |
| 125 | + mBorderColor = borderColor; | |
| 126 | + mBorderPaint.setColor(mBorderColor); | |
| 127 | + invalidate(); | |
| 128 | + } | |
| 129 | + | |
| 130 | + public int getBorderWidth() { | |
| 131 | + return mBorderWidth; | |
| 132 | + } | |
| 133 | + | |
| 134 | + public void setBorderWidth(int borderWidth) { | |
| 135 | + if (borderWidth == mBorderWidth) { | |
| 136 | + return; | |
| 137 | + } | |
| 138 | + | |
| 139 | + mBorderWidth = borderWidth; | |
| 140 | + setup(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Override | |
| 144 | + public void setImageBitmap(Bitmap bm) { | |
| 145 | + super.setImageBitmap(bm); | |
| 146 | + mBitmap = bm; | |
| 147 | + setup(); | |
| 148 | + } | |
| 149 | + | |
| 150 | + @Override | |
| 151 | + public void setImageDrawable(Drawable drawable) { | |
| 152 | + super.setImageDrawable(drawable); | |
| 153 | + mBitmap = getBitmapFromDrawable(drawable); | |
| 154 | + setup(); | |
| 155 | + } | |
| 156 | + | |
| 157 | + @Override | |
| 158 | + public void setImageResource(int resId) { | |
| 159 | + super.setImageResource(resId); | |
| 160 | + mBitmap = getBitmapFromDrawable(getDrawable()); | |
| 161 | + setup(); | |
| 162 | + } | |
| 163 | + | |
| 164 | + private Bitmap getBitmapFromDrawable(Drawable drawable) { | |
| 165 | + if (drawable == null) { | |
| 166 | + return null; | |
| 167 | + } | |
| 168 | + | |
| 169 | + if (drawable instanceof BitmapDrawable) { | |
| 170 | + return ((BitmapDrawable) drawable).getBitmap(); | |
| 171 | + } | |
| 172 | + | |
| 173 | + try { | |
| 174 | + Bitmap bitmap; | |
| 175 | + | |
| 176 | + if (drawable instanceof ColorDrawable) { | |
| 177 | + bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); | |
| 178 | + } else { | |
| 179 | + bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG); | |
| 180 | + } | |
| 181 | + | |
| 182 | + Canvas canvas = new Canvas(bitmap); | |
| 183 | + drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
| 184 | + drawable.draw(canvas); | |
| 185 | + return bitmap; | |
| 186 | + } catch (OutOfMemoryError e) { | |
| 187 | + return null; | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + private void setup() { | |
| 192 | + if (!mReady) { | |
| 193 | + mSetupPending = true; | |
| 194 | + return; | |
| 195 | + } | |
| 196 | + | |
| 197 | + if (mBitmap == null) { | |
| 198 | + return; | |
| 199 | + } | |
| 200 | + | |
| 201 | + mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); | |
| 202 | + | |
| 203 | + mBitmapPaint.setAntiAlias(true); | |
| 204 | + mBitmapPaint.setShader(mBitmapShader); | |
| 205 | + | |
| 206 | + mBorderPaint.setStyle(Paint.Style.STROKE); | |
| 207 | + mBorderPaint.setAntiAlias(true); | |
| 208 | + mBorderPaint.setColor(mBorderColor); | |
| 209 | + mBorderPaint.setStrokeWidth(mBorderWidth); | |
| 210 | + | |
| 211 | + mBitmapHeight = mBitmap.getHeight(); | |
| 212 | + mBitmapWidth = mBitmap.getWidth(); | |
| 213 | + | |
| 214 | + mBorderRect.set(0, 0, getWidth(), getHeight()); | |
| 215 | + mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2); | |
| 216 | + | |
| 217 | + mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth); | |
| 218 | + mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2); | |
| 219 | + | |
| 220 | + updateShaderMatrix(); | |
| 221 | + invalidate(); | |
| 222 | + } | |
| 223 | + | |
| 224 | + private void updateShaderMatrix() { | |
| 225 | + float scale; | |
| 226 | + float dx = 0; | |
| 227 | + float dy = 0; | |
| 228 | + | |
| 229 | + mShaderMatrix.set(null); | |
| 230 | + | |
| 231 | + if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) { | |
| 232 | + scale = mDrawableRect.height() / (float) mBitmapHeight; | |
| 233 | + dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f; | |
| 234 | + } else { | |
| 235 | + scale = mDrawableRect.width() / (float) mBitmapWidth; | |
| 236 | + dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f; | |
| 237 | + } | |
| 238 | + | |
| 239 | + mShaderMatrix.setScale(scale, scale); | |
| 240 | + mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth); | |
| 241 | + | |
| 242 | + mBitmapShader.setLocalMatrix(mShaderMatrix); | |
| 243 | + } | |
| 244 | + | |
| 245 | +} | |
| 246 | + | |
| 247 | + | ... | ... |
app/src/main/res/layout/commet_item.xml
| 1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
| 2 | -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 2 | +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 3 | 3 | android:layout_width="match_parent" |
| 4 | - android:layout_height="30dp" | |
| 5 | - > | |
| 6 | - <TextView | |
| 7 | - android:id="@+id/nick_name" | |
| 8 | - android:text="游客" | |
| 9 | - android:layout_width="wrap_content" | |
| 10 | - android:layout_height="wrap_content" | |
| 11 | - android:layout_marginRight="10dp"/> | |
| 12 | - | |
| 13 | - <TextView | |
| 14 | - android:layout_toRightOf="@+id/nick_name" | |
| 15 | - android:layout_toLeftOf="@+id/good_imv" | |
| 16 | - android:layout_marginLeft="5dp" | |
| 17 | - android:layout_marginRight="5dp" | |
| 18 | - android:id="@+id/comment_message" | |
| 19 | - android:text="我爱大中国" | |
| 20 | - android:layout_width="wrap_content" | |
| 21 | - android:layout_height="wrap_content" /> | |
| 22 | - | |
| 23 | - <ImageView android:id="@+id/good_imv" | |
| 24 | - android:layout_width="24dp" | |
| 25 | - android:layout_height="24dp" | |
| 26 | - android:src="@drawable/vod_detail_good" | |
| 27 | - android:layout_toLeftOf="@+id/comment_good_count" | |
| 28 | - android:layout_marginLeft="5dp" | |
| 29 | - android:layout_centerVertical="true" | |
| 30 | - android:clickable="true" | |
| 31 | - /> | |
| 32 | - | |
| 33 | - | |
| 34 | - | |
| 35 | - <TextView | |
| 36 | - android:id="@+id/comment_good_count" | |
| 37 | - android:text="10" | |
| 38 | - android:layout_centerVertical="true" | |
| 39 | - android:layout_width="wrap_content" | |
| 40 | - android:layout_height="wrap_content" | |
| 41 | - android:layout_alignParentRight="true" | |
| 42 | - android:layout_marginLeft="10dp" | |
| 43 | - android:layout_marginRight="10dp"/> | |
| 44 | - | |
| 45 | - | |
| 46 | -</RelativeLayout> | |
| 47 | 4 | \ No newline at end of file |
| 5 | + android:layout_height="wrap_content" | |
| 6 | + android:orientation="vertical" | |
| 7 | + > | |
| 8 | + | |
| 9 | + <RelativeLayout | |
| 10 | + android:layout_width="match_parent" | |
| 11 | + android:layout_height="wrap_content"> | |
| 12 | + | |
| 13 | + <com.cnlive.im.view.CircleImageView | |
| 14 | + android:id="@+id/img_head" | |
| 15 | + android:layout_width="30dp" | |
| 16 | + android:layout_height="30dp" | |
| 17 | + android:layout_centerVertical="true" | |
| 18 | + android:layout_marginLeft="5dp" | |
| 19 | + android:src="@mipmap/ic_launcher" /> | |
| 20 | + | |
| 21 | + | |
| 22 | + <TextView | |
| 23 | + android:id="@+id/nick_name" | |
| 24 | + android:layout_width="wrap_content" | |
| 25 | + android:layout_height="wrap_content" | |
| 26 | + android:layout_centerVertical="true" | |
| 27 | + android:layout_toRightOf="@+id/img_head" | |
| 28 | + android:layout_marginLeft="10dp" | |
| 29 | + android:text="游客" /> | |
| 30 | + | |
| 31 | + | |
| 32 | + <ImageView | |
| 33 | + android:id="@+id/good_imv" | |
| 34 | + android:layout_width="24dp" | |
| 35 | + android:layout_height="24dp" | |
| 36 | + android:layout_centerVertical="true" | |
| 37 | + android:layout_marginLeft="5dp" | |
| 38 | + android:layout_toLeftOf="@+id/comment_good_count" | |
| 39 | + android:clickable="true" | |
| 40 | + android:src="@drawable/vod_detail_good" /> | |
| 41 | + | |
| 42 | + | |
| 43 | + <TextView | |
| 44 | + android:id="@+id/comment_good_count" | |
| 45 | + android:layout_width="wrap_content" | |
| 46 | + android:layout_height="wrap_content" | |
| 47 | + android:layout_alignParentRight="true" | |
| 48 | + android:layout_centerVertical="true" | |
| 49 | + android:layout_marginLeft="10dp" | |
| 50 | + android:layout_marginRight="10dp" | |
| 51 | + android:text="10" /> | |
| 52 | + | |
| 53 | + </RelativeLayout> | |
| 54 | + | |
| 55 | + | |
| 56 | + <TextView | |
| 57 | + android:id="@+id/comment_message" | |
| 58 | + android:layout_width="wrap_content" | |
| 59 | + android:layout_height="wrap_content" | |
| 60 | + android:layout_marginTop="5dp" | |
| 61 | + android:paddingTop="5dp" | |
| 62 | + android:layout_marginLeft="10dp" | |
| 63 | + android:layout_marginRight="10dp" | |
| 64 | + android:text="我爱大中国" /> | |
| 65 | + | |
| 66 | + | |
| 67 | + <View | |
| 68 | + android:layout_width="match_parent" | |
| 69 | + android:layout_height="1px" | |
| 70 | + android:layout_marginTop="5dp" | |
| 71 | + android:background="#dcdcdc" /> | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | +</LinearLayout> | |
| 48 | 76 | \ No newline at end of file | ... | ... |
app/src/main/res/values/attrs.xml
0 → 100644