/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by vadim on 05.11.14.
*/
public class HideShowImgSet extends ImageView {
private static class ImgData {
Bitmap bitmap;
int width;
int height;
float x;
float y;
}
private int width, height;
private List<ImgData> imgSet = new ArrayList<ImgData>();
public HideShowImgSet(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
for (ImgData img: imgSet) {
img.y = img.y * h / oldh;
}
}
/**
* loads image, sets the width and height and horizontal pos
* vertically it will be placed so as it lies beyond the visible
* rect of this view
* @param resId img to load
* @param width
* @param height
* @param x horizontal pos
*/
public void addImage(int resId, int width, int height, float x) {
ImgData img = new ImgData();
img.bitmap = BitmapFactory.decodeResource(getResources(), resId);
img.bitmap = Bitmap.createScaledBitmap(img.bitmap, width, height, false);
img.x = x;
img.y = this.height + height;
img.width = width;
img.height = height;
imgSet.add(img);
}
/**
* moves the specified image
* @param index img index to move
* @param percent 0 means img lies beyound the lower border of the view, 1 means img bottom is
* aligned with view's bottom
*/
public void moveImg(int index, float percent) {
if (index < 0 || index >= imgSet.size()) return;
ImgData img = imgSet.get(index);
img.y = height - img.height * percent;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (ImgData img: imgSet) {
canvas.drawBitmap(img.bitmap, img.x, img.y, null);
}
}
}