我正在制作一个小项目,我试图在框架上设置图像,但问题是框架有不同的形式。所以我可以知道如何调整这些帧中的图像。
in xml ...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/blank"
android:orientation="vertical">
<LinearLayout
android:id="@+id/rel1"
android:layout_marginTop="13dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="25dp"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="fill_parent"
android:layout_weight="0.9" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/add" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="15dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.1" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</LinearLayout>
我使用的代码和我能够设置的图像
iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try {
yourBitmap = BitmapFactory.decodeFile(picturePath);
Bitmap resized= Bitmap.createScaledBitmap(yourBitmap, 300,300, true);
iv1.setImageBitmap(resized);
iv1.setScaleType(ScaleType.FIT_XY);
} catch (Exception e) {
e.printStackTrace();
}
}
更新1: 使用此代码后......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/blank"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/rel1"
android:layout_marginTop="20dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="25dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/add" />
</FrameLayout>
<LinearLayout
android:layout_marginTop="15dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
在Activity类
中 iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try {
yourBitmap = BitmapFactory.decodeFile(picturePath);
Bitmap resized= Bitmap.createScaledBitmap(yourBitmap, 200,300, true);
iv1.setImageBitmap(resized);
iv1.setScaleType(ScaleType.FIT_XY);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
也添加此行:
iv1.setImageBitmap(resized);
iv1.setScaleType(ScaleType.FIT_XY);
编辑后的对话:
从ImageView
删除FrameLayout
,我们将在源代码中动态制作。看一下Activity源代码:
public class PutPhoto extends Activity
{
ImageView imageView3, imageView4;
int width, height;
FrameLayout v;
private static int RESULT_LOAD_IMAGE1 = 1;
Bitmap yourBitmap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.put_photo);
v = (FrameLayout) findViewById(R.id.ff);
//v.setBackgroundDrawable(getResources().getDrawable(R.drawable.frame));
v.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
}
@SuppressLint("NewApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try
{
yourBitmap = BitmapFactory.decodeFile(picturePath);
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 200, 300, true);
imageView4 = new ImageView(this);
imageView3 = new ImageView(this);
imageView3.setImageDrawable(getResources().getDrawable(R.drawable.frame));
if(imageView3.getDrawable() != null)
{
width = imageView3.getDrawable().getMinimumWidth();
height = imageView3.getDrawable().getMinimumHeight();
}
Resources resources = this.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
if (width > metrics.widthPixels)
{
width = metrics.widthPixels;
height = width * height / imageView3.getDrawable().getMinimumWidth();
}
LayoutParams params=new LayoutParams(width, height);
v.addView(imageView3, params);
v.addView(imageView4, params);
imageView4.setImageBitmap(resized);
imageView4.setOnTouchListener(new Touch(PutPhoto.this));
imageView4.setVisibility(View.GONE);
imageView3.bringToFront();
imageView4.setVisibility(View.VISIBLE);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Touch.java
源代码,用于对从图库中拾取的图像应用缩放和旋转效果。
class Touch implements OnTouchListener
{
// These matrices will be used to move and zoom image
public static float degree = 0.0f;
Matrix matrix = new Matrix();
Context context;
// Matrix matrix1 = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
float[] lastEvent = null;
float d = 0f;
float newRot = 0f;
public Touch(Context context)
{
super();
this.context = context;
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f)
{
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
lastEvent = new float[4];
lastEvent[0] = event.getX(0);
lastEvent[1] = event.getX(1);
lastEvent[2] = event.getY(0);
lastEvent[3] = event.getY(1);
d = rotation(event);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG)
{
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
else if (mode == ZOOM && event.getPointerCount() == 2)
{
float newDist = spacing(event);
Log.v("SS", "Count=" + event.getPointerCount());
Log.v("SS", "newDist=" + newDist);
matrix.set(savedMatrix);
if (newDist > 10f)
{
float scale = newDist / oldDist;
System.out.println(newDist+" : "+oldDist+" : "+scale+" : "+mid.x+" : "+mid.y);
System.out.println("### Scale Value :: "+scale);
matrix.postScale(scale, scale, mid.x, mid.y);
}
if (lastEvent != null)
{
newRot = rotation(event);
Log.v("Degreeeeeeeeeee", "!!!!!!!!! newRot=" + (newRot));
degree = Math.abs(newRot);
float r = newRot - d;
Log.v("Degreeeeeeeeeee rrrrr ", "!!!!!!!!!!! rotate :: "+r);
matrix.postRotate(r, v.getMeasuredWidth() / 2, v.getMeasuredHeight() / 2);
}
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event)
{
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP)
{
sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++)
{
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
}
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event)
{
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
/** Determine the degree between the first two fingers */
private static float rotation(MotionEvent event)
{
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
// if (Constant.TRACE) Log.d("Rotation ~~~~~~~~~~~~~~~~~",
// delta_x+" ## "+delta_y+" ## "+radians+" ## "+Math.toDegrees(radians));
return (float) Math.toDegrees(radians);
}
}
答案 1 :(得分:0)
您需要在布局的XML中使用ScaleType。
答案 2 :(得分:0)
Try this
<LinearLayout
android:id="@+id/rel1"
android:layout_marginTop="13dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="25dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.9" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/add" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="15dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.1" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>