我想从图库中加载图像并在视图中居中,但不执行缩放。单击左,右,上和下按钮时,图像应在查看窗口中移动,步长约为5像素。
以下是我到目前为止所做的
Java代码
public class MainActivity extends AppCompatActivity {
final int PICK_IMAGE_REQUEST = 111;
boolean imageLoaded = false;
ImageView imgView;
boolean state = false;
Button up,down,left,right;
Bitmap inputBM, outputBM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up = (Button) findViewById(R.id.up);
left=(Button)findViewById(R.id.left);
right=(Button)findViewById(R.id.right);
down=(Button)findViewById(R.id.down);
final double contrastValue = 100;
up.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "UP", Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)imgView.getLayoutParams();
mParams.topMargin -= 5;
imgView.setLayoutParams(mParams);
}
});
down.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(),"DOWN",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
imgView.getLayoutParams();
mParams.topMargin += 20;
imgView.setLayoutParams(mParams);
}
});
left.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(),"LEFT",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
imgView.getLayoutParams();
mParams.leftMargin -= 20;
imgView.setLayoutParams(mParams);
}
});
right.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(),"RIGHT",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
imgView.getLayoutParams();
mParams.leftMargin += 20;
imgView.setLayoutParams(mParams);
}
});
}
public void loadImage(View view) {
Intent i = new Intent();
// Show only images, no videos or anything else
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(i, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
inputBM = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imgView = (ImageView) findViewById(R.id.image);
imgView.setImageBitmap(inputBM);
imgView.setScaleType(ImageView.ScaleType.CENTER);
imageLoaded = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
XML代码
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="360dp"
android:layout_height="320dp"
android:onClick="loadImage"
android:scaleType="centerCrop"
android:src="@drawable/no_image" />
<Button
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:text="Button" />
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="Button" />
<Button
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/right"
android:layout_toStartOf="@+id/up"
android:text="Button" />
<Button
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/up"
android:layout_marginTop="-100dp"
android:layout_toEndOf="@+id/up"
android:text="Button" />
但这不能按预期工作。它移动图像视图而不是图像视图内的图像。请对此提供帮助或提供其他解决方案来实现此目的。预先感谢。
答案 0 :(得分:0)
将这些更改放入XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="360dp"
android:layout_height="320dp"
android:gravity="center">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:onClick="loadImage"
android:scaleType="centerCrop" />
</RelativeLayout>
<Button
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:text="Button" />
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="Button" />
<Button
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/right"
android:layout_toStartOf="@+id/up"
android:text="Button" />
<Button
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/up"
android:layout_marginTop="-100dp"
android:layout_toEndOf="@+id/up"
android:text="Button" />
</RelativeLayout>
它仍然会使您的“图像视图”移动,但是您的视图将是图像的大小,因此会给人以图像正在移动的感觉。