下面列出了android.widget.ImageView#getDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Dialog createLoadingDialog(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(IdHelper.getLayout(context, "jmui_loading_view"), null);
RelativeLayout layout = (RelativeLayout) v.findViewById(IdHelper.getViewID(context, "jmui_dialog_view"));
ImageView mLoadImg = (ImageView) v.findViewById(IdHelper.getViewID(context, "jmui_loading_img"));
TextView mLoadText = (TextView) v.findViewById(IdHelper.getViewID(context, "jmui_loading_txt"));
AnimationDrawable mDrawable = (AnimationDrawable) mLoadImg.getDrawable();
mDrawable.start();
mLoadText.setText(msg);
final Dialog loadingDialog = new Dialog(context, IdHelper.getStyle(context, "jmui_loading_dialog_style"));
loadingDialog.setCancelable(true);
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
return loadingDialog;
}
@Override
public boolean setDisplayMatrix(Matrix finalMatrix) {
if (finalMatrix == null) {
throw new IllegalArgumentException("Matrix cannot be null");
}
ImageView imageView = getImageView();
if (null == imageView) {
return false;
}
if (null == imageView.getDrawable()) {
return false;
}
mSuppMatrix.set(finalMatrix);
setImageViewMatrix(getDrawMatrix());
checkMatrixBounds();
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = super.getView(position, convertView, parent);
//imageView.setImageDrawable(icons[position]);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
if (contactids.length > position) {
if (contactids[position] != null) {
Uri u = getPhotoUri(contactids[position]);
if (u != null) {
imageView.setImageURI(u);
if (imageView.getDrawable() == null)
imageView.setImageResource(R.drawable.ic_launcher);
//Log.d("gps1", u.toString());
} else {
//imageView.setImageResource(R.drawable.ic_contact_picture_2);
}
}
}
return rowView;
}
public static Dialog createLoadingDialog(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(IdHelper.getLayout(context, "jmui_loading_view"), null);
RelativeLayout layout = (RelativeLayout) v.findViewById(IdHelper.getViewID(context, "jmui_dialog_view"));
ImageView mLoadImg = (ImageView) v.findViewById(IdHelper.getViewID(context, "jmui_loading_img"));
TextView mLoadText = (TextView) v.findViewById(IdHelper.getViewID(context, "jmui_loading_txt"));
AnimationDrawable mDrawable = (AnimationDrawable) mLoadImg.getDrawable();
mDrawable.start();
mLoadText.setText(msg);
final Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);
loadingDialog.setCancelable(true);
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
return loadingDialog;
}
@Override
public boolean setDisplayMatrix(Matrix finalMatrix) {
if (finalMatrix == null) {
throw new IllegalArgumentException("Matrix cannot be null");
}
ImageView imageView = getImageView();
if (null == imageView) {
return false;
}
if (null == imageView.getDrawable()) {
return false;
}
mSuppMatrix.set(finalMatrix);
setImageViewMatrix(getDrawMatrix());
checkMatrixBounds();
return true;
}
protected static BitmapWorkerUrlTask getBitmapWorkerUrlTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawableUrl) {
final AsyncDrawableUrl asyncDrawable = (AsyncDrawableUrl) drawable;
return asyncDrawable.getBitmapWorkerUrlTask();
}
}
return null;
}
/**
* @param imageView Any imageView
* @return Retrieve the currently active task (if any) associated with this imageView.
* null if there is no such task.
*/
private BackgroundTask getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BackgroundTaskDrawable) {
BackgroundTaskDrawable downloadedDrawable = (BackgroundTaskDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
private static void setColor(ImageView image, TextView text, int color) {
// set color
GradientDrawable sd = (GradientDrawable) image.getDrawable();
sd.setColor(color);
image.invalidate();
// set color name
text.setText(getColorName(color));
}
/**
* Helper method that maps the supplied Matrix to the current Drawable
*
* @param matrix - Matrix to map Drawable against
* @return RectF - Displayed Rectangle
*/
private RectF getDisplayRect(Matrix matrix) {
ImageView imageView = getImageView();
if (null != imageView) {
Drawable d = imageView.getDrawable();
if (null != d) {
mDisplayRect.set(0, 0, d.getIntrinsicWidth(),
d.getIntrinsicHeight());
matrix.mapRect(mDisplayRect);
return mDisplayRect;
}
}
return null;
}
private Bitmap getBitmapFromImageView(ImageView imageView) {
Bitmap bitmap = null;
if (imageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
Drawable drawable = imageView.getDrawable();
if (drawable != null) {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
}
}
return bitmap;
}
public static int getPaletteColor(ImageView imageView) {
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Palette palette = Palette.generate(bitmap);
int x = Color.parseColor("#ea0101");
return palette.getVibrantColor(x);
}
private void recycleImage(View view) {
if (!(view instanceof ImageView)) {
ImageView imageView = (ImageView) view;
Drawable drawable = imageView.getDrawable();
if (drawable == null || !(drawable instanceof BitmapDrawable)) {
return;
} else {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
if (bitmap == null || bitmap.isRecycled()) {
return;
}
bitmap.recycle();
}
}
}
private static IPictureWorker getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof PictureBitmapDrawable) {
PictureBitmapDrawable downloadedDrawable = (PictureBitmapDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
private IPictureWorker getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof PictureBitmapDrawable) {
PictureBitmapDrawable downloadedDrawable = (PictureBitmapDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
/**
* 调整图片的frame
*
* @param imageView
*/
public static void adjustSize(ImageView imageView) {
if (imageView != null && imageView.getLayoutParams() != null && imageView.getDrawable() != null) {
int width = (imageView.getDrawable()).getIntrinsicWidth();
int height = (imageView.getDrawable()).getIntrinsicHeight();
if (width != imageView.getLayoutParams().width || height != imageView.getLayoutParams().height) {
imageView.getLayoutParams().width = width;
imageView.getLayoutParams().height = height;
imageView.requestLayout();
}
}
}
/**
* 返回的 宽高数组 是图片 在当前分辨率手机的宽高
*
* @param imageView
* @return
*/
public static float[] measureDrawableWH(final ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Matrix mar = imageView.getImageMatrix();
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();
return new float[]{intrinsicWidth, intrinsicHeight};
}
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
ImageView image = (ImageView) findViewById(R.id.image);
AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable();
animationDrawable.start();
}
/**
* @return true if the ImageView exists, and its Drawable exists
*/
private static boolean hasDrawable(ImageView imageView) {
return null != imageView && null != imageView.getDrawable();
}
public static Rect getBitmapRectFromImageView(ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
}
Rect rect = new Rect();
boolean isVisible = imageView.getGlobalVisibleRect(rect);
if (!isVisible) {
int[] location = new int[2];
imageView.getLocationOnScreen(location);
rect.left = location[0];
rect.top = location[1];
rect.right = rect.left + imageView.getWidth();
rect.bottom = rect.top + imageView.getHeight();
}
if (bitmap != null) {
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
int imageViewWidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
int imageviewHeight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();
float startScale;
if ((float) imageViewWidth / bitmapWidth > (float) imageviewHeight / bitmapHeight) {
// Extend start bounds horizontally
startScale = (float) imageviewHeight / bitmapHeight;
} else {
startScale = (float) imageViewWidth / bitmapWidth;
}
bitmapHeight = (int) (bitmapHeight * startScale);
bitmapWidth = (int) (bitmapWidth * startScale);
int deltaX = (imageViewWidth - bitmapWidth) / 2;
int deltaY = (imageviewHeight - bitmapHeight) / 2;
rect.set(rect.left + deltaX, rect.top + deltaY, rect.right - deltaX, rect.bottom - deltaY);
return rect;
} else {
return null;
}
}