package com.lx.aidlservice;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageViewSec extends ImageView {
private static final String TAG = "ImageViewSec";
private TextView mTextView;
private int mDrawableWidth;
private int mDrawableHeight;
private String mContent;
public String getContent() {
return mContent;
}
public void setContent(String content) {
this.mContent = content;
}
public ImageViewSec(Context context) {
super(context);
initTextView(context);
}
public ImageViewSec(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ImageViewSec(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private void initTextView(Context context) {
if (null == mTextView) {
mTextView = new TextView(context);
}
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
if (null != drawable) {
updateDrawable(drawable);
}
}
private void updateDrawable(Drawable d) {
if (d != null) {
mDrawableWidth = d.getIntrinsicWidth();
mDrawableHeight = d.getIntrinsicHeight();
configureBounds();
} else {
mDrawableWidth = mDrawableHeight = -1;
}
}
private void configureBounds() {
if (null == getDrawable()) {
return;
}
int dwidth = mDrawableWidth;
int dheight = mDrawableHeight;
int vwidth = getWidth() - getPaddingLeft() - getPaddingRight();
int vheight = getHeight() - getPaddingTop() - getPaddingBottom();
if (ScaleType.CENTER_CROP == getScaleType()) {
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
Log.d(TAG, "configureBounds scale=" + scale + " vwidth=" + vwidth + " vheight="
+ vheight + " dwidth=" + dwidth + " dheight=" + dheight);
}
}
The column labeled "fill_parent x fill_parent" represents the following layout:
The column labeled "200dp x 200dp" represents the following layout:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageViewSec extends ImageView {
private static final String TAG = "ImageViewSec";
private TextView mTextView;
private int mDrawableWidth;
private int mDrawableHeight;
private String mContent;
public String getContent() {
return mContent;
}
public void setContent(String content) {
this.mContent = content;
}
public ImageViewSec(Context context) {
super(context);
initTextView(context);
}
public ImageViewSec(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ImageViewSec(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private void initTextView(Context context) {
if (null == mTextView) {
mTextView = new TextView(context);
}
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
if (null != drawable) {
updateDrawable(drawable);
}
}
private void updateDrawable(Drawable d) {
if (d != null) {
mDrawableWidth = d.getIntrinsicWidth();
mDrawableHeight = d.getIntrinsicHeight();
configureBounds();
} else {
mDrawableWidth = mDrawableHeight = -1;
}
}
private void configureBounds() {
if (null == getDrawable()) {
return;
}
int dwidth = mDrawableWidth;
int dheight = mDrawableHeight;
int vwidth = getWidth() - getPaddingLeft() - getPaddingRight();
int vheight = getHeight() - getPaddingTop() - getPaddingBottom();
if (ScaleType.CENTER_CROP == getScaleType()) {
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
Log.d(TAG, "configureBounds scale=" + scale + " vwidth=" + vwidth + " vheight="
+ vheight + " dwidth=" + dwidth + " dheight=" + dheight);
}
}
}
looking for.
Here is a table showing samples for all scale types for the ImageView.
scaleType | fill_parent x fill_parent | 200dp x 200dp | 200dp x 200dp with Overlay |
---|---|---|---|
| |||
| |||
| |||
| |||
| |||
| |||
| |||
|
The column labeled "fill_parent x fill_parent" represents the following layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/eureka"
android:scaleType="matrix">
</ImageView>
</FrameLayout>
The column labeled "200dp x 200dp" represents the following layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/eureka"
android:scaleType="matrix">
</ImageView>
</FrameLayout>