首页 > 代码库 > 2、列表item_圆头像_信息提示

2、列表item_圆头像_信息提示

 1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.ListView; 8 import android.widget.Toast; 9 import com.dk.view.drop.CoverManager;10 import com.dk.view.drop.WaterDrop;11 import com.dk.view.drop.DropCover.OnDragCompeteListener;12 13 public class MainActivity extends Activity {14 15     @Override16     protected void onCreate(Bundle savedInstanceState) {17         super.onCreate(savedInstanceState);18         setContentView(R.layout.activity_main);19         getActionBar().hide();20         CoverManager.getInstance().init(this);21 22         ListView mList = (ListView) findViewById(R.id.list);23         mList.setAdapter(new DemoAdapter());24 25         CoverManager.getInstance().setMaxDragDistance(150);26         CoverManager.getInstance().setExplosionTime(150);27     }28 29     class DemoAdapter extends BaseAdapter {30         @Override31         public int getCount() {32             return 99;33         }34         @Override35         public Object getItem(int position) {36             return null;37         }38         @Override39         public long getItemId(int position) {40             return 0;41         }42         @Override43         public View getView(final int position, View convertView,44                 ViewGroup parent) {45             convertView = LayoutInflater.from(MainActivity.this).inflate(46                     R.layout.view_list_item, null);47         WaterDrop drop = (WaterDrop) convertView.findViewById(R.id.drop);48         drop.setText(String.valueOf(position));49         drop.setOnDragCompeteListener(new OnDragCompeteListener() {52                 @Override53                 public void onDrag() {54                   Toast.makeText(MainActivity.this, "remove:" + position,55                             Toast.LENGTH_SHORT).show();56                 }57             });58             return convertView;59         }60     }61 }
 1 <?xml version="1.0" encoding="utf-8"?> 2  3 <!-- R.layout.view_list_item --> 4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5     xmlns:app="http://schemas.android.com/apk/res/com.view.drop" 6     android:layout_width="match_parent" 7     android:layout_height="match_parent" 8     android:gravity="center_vertical" 9     android:orientation="horizontal" >10 11     <com.view.drop.CircleImageView12         android:layout_width="50dp"13         android:layout_height="50dp"14         android:src="@drawable/p3"15         app:border_color="#92DDED"16         app:border_width="3dp" />17 18     <LinearLayout19         android:layout_width="match_parent"20         android:layout_height="60dp"21         android:orientation="vertical" >22         <LinearLayout23             android:layout_width="match_parent"24             android:layout_height="30dp"25             android:layout_marginLeft="10dp"26             android:layout_marginRight="10dp"27             android:gravity="center_vertical" >28             <TextView29                 android:layout_width="match_parent"30                 android:layout_height="30dp"31                 android:layout_weight="1"32                 android:gravity="center_vertical"33                 android:text="Dean Ding"34                 android:textColor="#444444" />35             <TextView36                 android:layout_width="40dp"37                 android:layout_height="30dp"38                 android:gravity="center_vertical"39                 android:text="11:11"40                 android:textColor="#666666" />41         </LinearLayout>42 43         <LinearLayout44             android:layout_width="match_parent"45             android:layout_height="30dp"46             android:layout_marginLeft="10dp"47             android:layout_marginRight="10dp"48             android:gravity="center_vertical" >49             <TextView50                 android:layout_width="match_parent"51                 android:layout_height="30dp"52                 android:layout_weight="1"53                 android:gravity="center_vertical"54                 android:text="Hello World"55                 android:textColor="#666666" />56 57             <com.dk.view.drop.WaterDrop58                 android:id="@+id/drop"59                 android:layout_width="25dp"60                 android:layout_height="25dp"61                 android:gravity="center_vertical" />62         </LinearLayout>63     </LinearLayout>64 65 </LinearLayout>

 

  1 import android.content.Context;  2 import android.content.res.TypedArray;  3 import android.graphics.Bitmap;  4 import android.graphics.BitmapShader;  5 import android.graphics.Canvas;  6 import android.graphics.Color;  7 import android.graphics.Matrix;  8 import android.graphics.Paint;  9 import android.graphics.RectF; 10 import android.graphics.Shader; 11 import android.graphics.drawable.BitmapDrawable; 12 import android.graphics.drawable.ColorDrawable; 13 import android.graphics.drawable.Drawable; 14 import android.util.AttributeSet; 15 import android.widget.ImageView; 16  17 /** 18  * Copyright 2014 Henning Dodenhof 19  *  20  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 21  * use this file except in compliance with the License. You may obtain a copy of 22  * the License at 23  *  24  * http://www.apache.org/licenses/LICENSE-2.0 25  *  26  * Unless required by applicable law or agreed to in writing, software 27  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 28  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 29  * License for the specific language governing permissions and limitations under 30  * the License. 31  *  32  * 圆头像 33  *  34  */ 35 public class CircleImageView extends ImageView { 36  37     private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; 38  39     private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888; 40     private static final int COLORDRAWABLE_DIMENSION = 1; 41  42     private static final int DEFAULT_BORDER_WIDTH = 0; 43     private static final int DEFAULT_BORDER_COLOR = Color.BLACK; 44  45     private final RectF mDrawableRect = new RectF(); 46     private final RectF mBorderRect = new RectF(); 47  48     private final Matrix mShaderMatrix = new Matrix(); 49     private final Paint mBitmapPaint = new Paint(); 50     private final Paint mBorderPaint = new Paint(); 51  52     private int mBorderColor = DEFAULT_BORDER_COLOR; 53     private int mBorderWidth = DEFAULT_BORDER_WIDTH; 54  55     private Bitmap mBitmap; 56     private BitmapShader mBitmapShader; 57     private int mBitmapWidth; 58     private int mBitmapHeight; 59  60     private float mDrawableRadius; 61     private float mBorderRadius; 62  63     private boolean mReady; 64     private boolean mSetupPending; 65  66     public CircleImageView(Context context) { 67         super(context); 68     } 69  70     public CircleImageView(Context context, AttributeSet attrs) { 71         this(context, attrs, 0); 72     } 73  74     public CircleImageView(Context context, AttributeSet attrs, int defStyle) { 75         super(context, attrs, defStyle); 76         super.setScaleType(SCALE_TYPE); 77  78         TypedArray a = context.obtainStyledAttributes(attrs, 79                 R.styleable.CircleImageView, defStyle, 0); 80  81         mBorderWidth = a.getDimensionPixelSize( 82                 R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH); 83         mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, 84                 DEFAULT_BORDER_COLOR); 85  86         a.recycle(); 87  88         mReady = true; 89  90         if (mSetupPending) { 91             setup(); 92             mSetupPending = false; 93         } 94     } 95  96     @Override 97     public ScaleType getScaleType() { 98         return SCALE_TYPE; 99     }100 101     @Override102     public void setScaleType(ScaleType scaleType) {103         if (scaleType != SCALE_TYPE) {104             throw new IllegalArgumentException(String.format(105                     "ScaleType %s not supported.", scaleType));106         }107     }108 109     @Override110     protected void onDraw(Canvas canvas) {111         if (getDrawable() == null) {112             return;113         }114 115         canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,116                 mBitmapPaint);117         if (mBorderWidth != 0) {118             canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,119                     mBorderPaint);120         }121     }122 123     @Override124     protected void onSizeChanged(int w, int h, int oldw, int oldh) {125         super.onSizeChanged(w, h, oldw, oldh);126         setup();127     }128 129     public int getBorderColor() {130         return mBorderColor;131     }132 133     public void setBorderColor(int borderColor) {134         if (borderColor == mBorderColor) {135             return;136         }137 138         mBorderColor = borderColor;139         mBorderPaint.setColor(mBorderColor);140         invalidate();141     }142 143     public int getBorderWidth() {144         return mBorderWidth;145     }146 147     public void setBorderWidth(int borderWidth) {148         if (borderWidth == mBorderWidth) {149             return;150         }151 152         mBorderWidth = borderWidth;153         setup();154     }155 156     @Override157     public void setImageBitmap(Bitmap bm) {158         super.setImageBitmap(bm);159         mBitmap = bm;160         setup();161     }162 163     @Override164     public void setImageDrawable(Drawable drawable) {165         super.setImageDrawable(drawable);166         mBitmap = getBitmapFromDrawable(drawable);167         setup();168     }169 170     @Override171     public void setImageResource(int resId) {172         super.setImageResource(resId);173         mBitmap = getBitmapFromDrawable(getDrawable());174         setup();175     }176 177     private Bitmap getBitmapFromDrawable(Drawable drawable) {178         if (drawable == null) {179             return null;180         }181 182         if (drawable instanceof BitmapDrawable) {183             return ((BitmapDrawable) drawable).getBitmap();184         }185 186         try {187             Bitmap bitmap;188 189             if (drawable instanceof ColorDrawable) {190                 bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,191                         COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);192             } else {193                 bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),194                         drawable.getIntrinsicHeight(), BITMAP_CONFIG);195             }196 197             Canvas canvas = new Canvas(bitmap);198             drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());199             drawable.draw(canvas);200             return bitmap;201         } catch (OutOfMemoryError e) {202             return null;203         }204     }205 206     private void setup() {207         if (!mReady) {208             mSetupPending = true;209             return;210         }211 212         if (mBitmap == null) {213             return;214         }215 216         mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,217                 Shader.TileMode.CLAMP);218 219         mBitmapPaint.setAntiAlias(true);220         mBitmapPaint.setShader(mBitmapShader);221 222         mBorderPaint.setStyle(Paint.Style.STROKE);223         mBorderPaint.setAntiAlias(true);224         mBorderPaint.setColor(mBorderColor);225         mBorderPaint.setStrokeWidth(mBorderWidth);226 227         mBitmapHeight = mBitmap.getHeight();228         mBitmapWidth = mBitmap.getWidth();229 230         mBorderRect.set(0, 0, getWidth(), getHeight());231         mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2,232                 (mBorderRect.width() - mBorderWidth) / 2);233 234         mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width()235                 - mBorderWidth, mBorderRect.height() - mBorderWidth);236         mDrawableRadius = Math.min(mDrawableRect.height() / 2,237                 mDrawableRect.width() / 2);238 239         updateShaderMatrix();240         invalidate();241     }242 243     private void updateShaderMatrix() {244         float scale;245         float dx = 0;246         float dy = 0;247 248         mShaderMatrix.set(null);249 250         if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width()251                 * mBitmapHeight) {252             scale = mDrawableRect.height() / (float) mBitmapHeight;253             dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;254         } else {255             scale = mDrawableRect.width() / (float) mBitmapWidth;256             dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;257         }258 259         mShaderMatrix.setScale(scale, scale);260         mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth,261                 (int) (dy + 0.5f) + mBorderWidth);262 263         mBitmapShader.setLocalMatrix(mShaderMatrix);264     }265 266 }

 

  1 import com.dk.view.drop.DropCover.OnDragCompeteListener;  2 import android.annotation.SuppressLint;  3 import android.content.Context;  4 import android.graphics.Canvas;  5 import android.graphics.Paint;  6 import android.os.Build.VERSION;  7 import android.util.AttributeSet;  8 import android.view.MotionEvent;  9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.ListView; 12 import android.widget.RelativeLayout; 13 import android.widget.ScrollView; 14 import android.widget.TextView; 15 // 红色圆圈 16 public class WaterDrop extends RelativeLayout { 17     private Paint mPaint = new Paint(); 18     private TextView mTextView; 19     private DropCover.OnDragCompeteListener mOnDragCompeteListener; 20     private boolean mHolderEventFlag; 21  22     public WaterDrop(Context context) { 23         super(context); 24         init(); 25     } 26  27     public WaterDrop(Context context, AttributeSet attrs) { 28         super(context, attrs); 29         init(); 30     } 31  32     public void setText(String str) { 33         mTextView.setText(str); 34     } 35  36     public void setTextSize(int size) { 37         mTextView.setTextSize(size); 38     } 39  40     @SuppressLint("NewApi") 41     private void init() { 42         mPaint.setAntiAlias(true); 43         if (VERSION.SDK_INT > 11) { 44             setLayerType(View.LAYER_TYPE_SOFTWARE, null); 45         } 46  47         mTextView = new TextView(getContext()); 48         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 49         params.addRule(RelativeLayout.CENTER_IN_PARENT); 50         mTextView.setTextSize(13); 51         mTextView.setTextColor(0xffffffff); 52         mTextView.setLayoutParams(params); 53         addView(mTextView); 54     } 55  56     @Override 57     protected void dispatchDraw(Canvas canvas) { 59         mPaint.setColor(0xffff0000); 60         canvas.drawCircle(getWidth() / 2, getHeight() / 2, 
getWidth() / 2, mPaint); 61 super.dispatchDraw(canvas); 62 } 63 64 @Override 65 protected void onDraw(Canvas canvas) { 66 super.onDraw(canvas); 67 } 68 69 @Override 70 public boolean onTouchEvent(MotionEvent event) { 71 ViewGroup parent = getScrollableParent(); 72 switch (event.getAction()) { 73 case MotionEvent.ACTION_DOWN: 74 mHolderEventFlag = !CoverManager.getInstance().isRunning(); 75 if (mHolderEventFlag) { 76 if (parent != null) 77 parent.requestDisallowInterceptTouchEvent(true); 78 CoverManager.getInstance().start(this, event.getRawX(), event.getRawY(), mOnDragCompeteListener); 79 } 80 break; 81 case MotionEvent.ACTION_MOVE: 82 if (mHolderEventFlag) { 83 CoverManager.getInstance().update(event.getRawX(), event.getRawY()); 84 } 85 break; 86 case MotionEvent.ACTION_UP: 87 case MotionEvent.ACTION_CANCEL: 88 if (mHolderEventFlag) { 89 if (parent != null) 90 parent.requestDisallowInterceptTouchEvent(false); 91 CoverManager.getInstance().finish(this, event.getRawX(), event.getRawY()); 92 } 93 break; 94 } 95 96 return true; 97 } 98 99 private ViewGroup getScrollableParent() {100 View target = this;101 while (true) {102 View parent;103 try {104 /**105 * ViewRootImpl cannot be cast to android.view.View106 */107 parent = (View) target.getParent();108 } catch (Exception e) {109 return null;110 }111 if (parent == null)112 return null;113 if (parent instanceof ListView || parent instanceof ScrollView) {114 return (ViewGroup) parent;115 }116 target = parent;117 }118 119 }120 121 public void setOnDragCompeteListener(OnDragCompeteListener
onDragCompeteListener) {
122 mOnDragCompeteListener = onDragCompeteListener;123 }124 }

 

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

 

WaterDropLibrary  http://download.csdn.net/detail/androidsj/7948845

2、列表item_圆头像_信息提示