Sho*_*que 13 android offset coordinates imageview android-recyclerview
我需要在Frame (多个图像帧)中显示一些图像,我有左边和顶部坐标对应每个图像.
例如,我有左边的图像坐标100和顶部的100,所以我需要做的只是在ImageView中显示从(100,100)开始的图像.
任何人都可以告诉我如何在没有Bitmap的情况下这样做,因为我需要在RecyclerView中显示带有多个图像的帧列表.
我用于TouchImageView.class缩放和移动功能来创建框架.
1.在我选择布局时,在屏幕截图下方,您可以清楚地看到两个图像彼此非常接近.我的意思是我选择每个图像的一半.
2.接下来是主屏幕,我需要重新绘制该图像,我有两个图像的左侧和顶部坐标.但我如何通过一些自定义x和y在ImageView中绘制图像.
用于在左侧和顶部填充ImageView内部图像的代码段 -
img.setPadding((int) x, (int) y, 0, 0);
Run Code Online (Sandbox Code Playgroud)
但仍然没有工作.
请给我一些建议,我已经完成谷歌搜索,但没有得到适合我的要求的解决方案.
建议真的很感激.
提前谢谢了.
亲切的问候.
更新:我在本文末尾添加了一个示例应用程序来说明这个概念.
第二次更新:为示例应用添加了一些代码以适应xy缩放
我已经更改了示例应用程序,以包含适合图像的代码,无论是小于还是大于ImageView视图大小,同时尊重宽高比.这可能不是你想要的,但它应该让你进入球场.这是示例代码,因此确实可以在实际应用程序中容纳其他内容.例如,xScale并且yScale可以继续在极端情况下负.我不确定如果他们这样做会发生什么,但崩溃可能是结果,所以边界检查是有序的.
如果我了解您要执行的操作,则需要使用固定大小的ImageView通过将图像的自定义(x,y)坐标设置为位于ImageView的(0,0)坐标来显示较大图像的不同部分.这基本上是在顶部和左边缘上裁剪图像.
有很多不同的方法可以做到这一点,哪一个最适合你将取决于你的确切需求,但这是一种方式:
在代码中使用以下或某些变体:
float xOffset = -100;
float yOffset = -100;
ImageView imageView = (ImageView) findViewById(R.id.shiftedView);
Matrix matrix = new Matrix();
matrix.setTranslate(xOffset, yOffset);
imageView.setImageMatrix(matrix);
Run Code Online (Sandbox Code Playgroud)
设置ImageView,如下所示.这里的关键是android:scaleType="matrix"
<ImageView
android:id="@+id/shiftedView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="#FF00CCCC"
android:scaleType="matrix"
android:src="@drawable/image" />
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你.
示例应用程序
这是这个概念的快速模型.这个应用程序由主要活动和一个布局组成.您必须提供一个图像作为ImageView查看其工作原理的源.
七个按钮将在图像的布局中左右,上下移动图像ImageView."Fit X"和"Fit Y"按钮将图像缩放到相应的尺寸范围内ImageView."重置"按钮将图像设置回原始默认设置ImageView.
MainActivity.java
package com.example.imageshift;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
float xOffset = 0f;
float yOffset = 0f;
float xScale = 1.0f;
float yScale = 1.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.shiftedView);
findViewById(R.id.shiftUp).setOnClickListener(this);
findViewById(R.id.shiftDown).setOnClickListener(this);
findViewById(R.id.shiftLeft).setOnClickListener(this);
findViewById(R.id.shiftRight).setOnClickListener(this);
findViewById(R.id.shiftReset).setOnClickListener(this);
findViewById(R.id.fitX).setOnClickListener(this);
findViewById(R.id.fitY).setOnClickListener(this);
}
public void doMatrixTransformations() {
Matrix matrix = new Matrix();
matrix.setTranslate(xOffset, yOffset);
matrix.postScale(xScale, yScale);
imageView.setImageMatrix(matrix);
}
@Override
public void onClick(View view) {
final int shiftAmount = 50;
switch (view.getId()) {
case R.id.shiftUp:
yOffset -= shiftAmount;
break;
case R.id.shiftDown:
yOffset += shiftAmount;
break;
case R.id.shiftLeft:
xOffset -= shiftAmount;
break;
case R.id.shiftRight:
xOffset += shiftAmount;
break;
case R.id.fitX:
xScale = (float) imageView.getWidth() / (
(float) imageView.getDrawable().getIntrinsicWidth() + xOffset);
yScale = xScale;
break;
case R.id.fitY:
yScale = (float) imageView.getHeight() /
((float) imageView.getDrawable().getIntrinsicHeight() + yOffset);
xScale = yScale;
break;
case R.id.shiftReset:
xOffset = 0;
yOffset = 0;
xScale = 1.0f;
yScale = 1.0f;
break;
}
doMatrixTransformations();
}
}
Run Code Online (Sandbox Code Playgroud)
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
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="com.example.imageshift.MainActivity">
<ImageView
android:id="@+id/shiftedView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:padding="2dp"
android:scaleType="matrix"
android:src="@drawable/image" />
<Button
android:id="@+id/shiftUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Up" />
<Button
android:id="@+id/shiftDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/shiftUp"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Down" />
<Button
android:id="@+id/shiftLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/shiftDown"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Left" />
<Button
android:id="@+id/shiftRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/shiftLeft"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Right" />
<Button
android:id="@+id/fitX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/shiftRight"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Fit X" />
<Button
android:id="@+id/fitY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fitX"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Fit Y" />
<Button
android:id="@+id/shiftReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fitY"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/shiftedView"
android:text="Reset" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
最终我找到了我的问题的解决方案 -
感谢您提供的所有帮助。
所以,基本上我有视图高度,视图宽度,顶部坐标,左侧坐标,原始图像高度和原始图像宽度。
这是其公式 -
float newAspectFactor = 0;
if (viewHeight > viewWidth) {
//Aspect factor for Height
newAspectFactor = viewHeight / bean.getPostMedia().get(i).getImg_height();
} else {
//Aspect factor for Width
newAspectFactor = viewWidth / bean.getPostMedia().get(i).getImg_width();
}
float imgNewHeight = newAspectFactor * imageHeight;
float imgNewWidth = newAspectFactor * imageWidth;
Logger.logsError(TAG, " Image New Height : " + imgNewHeight);
Logger.logsError(TAG, " Image New Width : " + imgNewWidth);
if (imgNewHeight < viewHeight) {
newAspectFactor = viewHeight / bean.getPostMedia().get(i).getImg_height();
} else if (imgNewWidth < viewWidth) {
newAspectFactor = viewWidth / bean.getPostMedia().get(i).getImg_width();
}
Run Code Online (Sandbox Code Playgroud)
顶部和左侧坐标。
float x = 42.0;
float y = 0.0;
Run Code Online (Sandbox Code Playgroud)魔法从这里开始。
Matrix m = img.getImageMatrix();
RectF drawableRect = new RectF(x,
y,
((x * newAspectFactor) + viewWidth) / newAspectFactor,
imageHeight);
RectF viewRect = new RectF(0,
0,
viewWidth,
viewHeight);
m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL);
img.setImageMatrix(m);
img.setScaleType(ImageView.ScaleType.MATRIX);
imageLoaderNew.displayImage("IMAGE URL", img, optionsPostImg);
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1393 次 |
| 最近记录: |