liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - ImageView

 

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/plus_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度+"/>
<Button
android:id="@+id/minus_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度-"/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="次の画像" />
</LinearLayout>
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="280dp"
android:scaleType="fitCenter"
android:src="@drawable/a"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#0000ff"
android:layout_margin="10pt"
/>
</LinearLayout>
 

 

MainActivity.xml
 
package com.example.liguofeng.imageview;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

private int[] images = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e
};

private Button next;
private int currentImage = 0; // 現在の画像
private int alpha = 255; // 透明度

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

next = (Button) findViewById(R.id.next);
final Button plus = (Button) findViewById(R.id.plus_alpha);
final Button minus = (Button) findViewById(R.id.minus_alpha);

final ImageView image1 = (ImageView) findViewById(R.id.image1);
final ImageView image2 = (ImageView) findViewById(R.id.image2);

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 次の画像表示
image1.setImageResource(images[++currentImage % images.length]);
}
});

View.OnClickListener listner = new View.OnClickListener()
{@Override
public void onClick(View e) {
if (e == plus) {
alpha += 20;
}
if (e == minus) {
alpha -= 20;
}
if (alpha >= 255) {
alpha = 255;
}
if(alpha <= 0) {
alpha = 0;
}

// 透明度変更
image1.setImageAlpha(alpha);
}
};

// イベント設定
plus.setOnClickListener(listner);
minus.setOnClickListener(listner);

image1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();

// bitmap実際サイズとimages1の伸縮比例
double scale = 1.0 * bitmap.getHeight() / image1.getHeight();

int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);

if( x + 120 > bitmap.getWidth() ) {
x = bitmap.getWidth() - 120;
}
if(y + 120 > bitmap.getHeight() ) {
y = bitmap.getHeight() - 120;
}

// image2設定
image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
image2.setImageAlpha(alpha);

return false;
}
});
}
}