gpt4 book ai didi

android - 无法在 Android 中的同一 UI 上显示缩放图像和其他内容?

转载 作者:行者123 更新时间:2023-11-29 21:16:28 25 4
gpt4 key购买 nike

我是 android 的新手,有人可以帮助我吗,我正在学习本教程 ZOOM IMAGEVIEW CLASS我的问题是我想点击一个 ImageView ,点击它会移动到下一个 Activity ,它将在上半部分显示先前点击的 Activity 缩放图像,在其余部分显示其余内容,但现在我无法加载我的 XML 文件这里通过使用本教程。它只是缩放图像

代码在这里:

 protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.sample); // i want to show this layout how can i show zoom image in this layout.
Intent intent = getIntent();
String stringData = intent.getStringExtra("imageName");//this is image file name
Log.i("stringData",""+stringData);

String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+stringData , null, null);
System.out.println("IMG ID :: "+imgId);
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
TouchImageView touch = new TouchImageView(this);
touch.setImageBitmap(bitmap);
touch.setMaxZoom(4f); //change the max level of zoom, default is 3f

//ImageView image = (ImageView)findViewById(R.id.img1);
//image.setImageBitmap(bitmap);
setContentView(touch);
}

这是 Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/img1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon_ask_fatwa_one" />

<LinearLayout
android:id="@+id/layout_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/btn_email_fatwa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/email"
android:layout_weight="1"/>
<ImageView
android:id="@+id/btn_share_fatwa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/share_fatwa"
android:layout_weight="1"/>
<ImageView
android:id="@+id/btn_save_fatwa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/save_fatwa"
android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

最佳答案

您正在使用 setContentView(touch);,它隐藏了您之前设置的 setContentView(R.layout.sample);
您需要做的就是像这样将此 TouchImageView 放入您的 sample.xml 布局文件中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/img1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon_ask_fatwa_one" />

<com.example.zoomimagesample.TouchImageView
android:id="@+id/YOUR_DESIRED_ID"
android:layout_width="fill_parent"
android:layout_height="OUR_DESIRED_HEIGHT"
/>

<LinearLayout
android:id="@+id/layout_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/btn_email_fatwa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/email"
android:layout_weight="1"/>
<ImageView
android:id="@+id/btn_share_fatwa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/share_fatwa"
android:layout_weight="1"/>
<ImageView
android:id="@+id/btn_save_fatwa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/save_fatwa"
android:layout_weight="1"/>

</LinearLayout>


然后像这样编辑你的onCreate(...)方法:

protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.sample); // i want to show this layout how can i show zoom image in this layout.
Intent intent = getIntent();
String stringData = intent.getStringExtra("imageName");//this is image file name
Log.i("stringData",""+stringData);

String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+stringData , null, null);
System.out.println("IMG ID :: "+imgId);
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
TouchImageView touch = (TouchImageView)findViewById(R.id.ID_THAT_YOU_ASSIGNED_TO_THE_TOUCH_IMAGE)VIEW);
touch.setImageBitmap(bitmap);
touch.setMaxZoom(4f); //change the max level of zoom, default is 3f

//ImageView image = (ImageView)findViewById(R.id.img1);
//image.setImageBitmap(bitmap);
//setContentView(touch);
}


编辑:
您缺少用于通过 XML 调用 View 的构造函数:

这里添加这两个构造函数:

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);

setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);

matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = ZOOM;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
Log.d("******", "ZOOM OR DRAG");
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);

if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}else if(mode == DRAG && saveScale == minScale) {
Log.d("******", "DRAG");
}
break;

case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;

case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}

});
}

public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);

setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);

matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = ZOOM;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
Log.d("******", "ZOOM OR DRAG");
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);

if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}else if(mode == DRAG && saveScale == minScale) {
Log.d("******", "DRAG");
}
break;

case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;

case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}

});
}

希望对您有所帮助。

关于android - 无法在 Android 中的同一 UI 上显示缩放图像和其他内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21312718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com