Android: how set image view outside of the activity - android

ImageRenderer.java
public class ImageRenderer implements Renderer {
...
private Context mActivityContext;
...
public SandAniRenderer(final Context activityContext) {
// TODO Auto-generated constructor stub
mActivityContext = activityContext;
mField = new Field(mActivityContext);
...
}
private boolean InitializeObject(int width, int height)
{
...
Field.SetField(width, height, R.drawable.image);
...
return true;
}
Field.java
public class Field extends xxx{
private final Context mActivityContext;
public Field(Context activityContext)
{
mActivityContext = activityContext;
}
public boolean SetField(int width, int height, int fn)
{
ImageView mImage = (ImageView) ((Activity) mActivityContext).findViewById(fn);
...
}
but it doesn't work!
mImage contains null image...
I can't find how can image contain outside of the activity.
Can you help me about how to implement drawing an ImageView outside of the activity?

Assuming mField is a member variable,
public class ImageRenderer implements Renderer {
Field mField = null;
use that in InitializeObject. Your Context is null because it wasn't set when you called it statically.
private boolean InitializeObject(int width, int height)
{
...
mField.SetField(width, height, R.drawable.image);
...
return true;
}
After you have set
mField = new Field(mActivityContext);

Try this (emptyimage is the image i have in my drawable)
mImage.setImageResource(R.drawable.emptyimage);

Related

Android - Implement security in WallpaperService

I am using the WallpaperService class to set a live wallpaper on the device.
I want to implement security on the system screen (to prevent screenshot or recording) where the 'Set Wallpaper' button shows up by the android system.
So far, I have found one method of SurfaceView class - surfaceview.setSecure(boolean value)
But, I am not able to get the instance of the SurfaceView inside my class.
Please suggest some workaround to get the instance of this class.
My Code-
public class LiveWallpaperService extends WallpaperService {
private int mDeviceWidth, mDeviceHeight;
private int mAnimationWidth, mAnimationHeight;
#Override
public Engine onCreateEngine() {
Movie movie = null;
// Some Code here
return new GIFWallpaperEngine(movie);
}
private class GIFWallpaperEngine extends Engine {
private final int frameDuration = 24;
private SurfaceHolder holder;
private final Movie movie;
private boolean visible;
private final Handler handler;
private final Runnable drawGIF = new Runnable() {
public void run() {
draw();
}
};
public SurfaceView getSurfaceView(){
// How to find the SurfaceView object here?
}
GIFWallpaperEngine(Movie movie) {
this.movie = movie;
handler = new Handler();
}
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
this.holder = surfaceHolder;
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
mDeviceWidth = width;
mDeviceHeight = height;
}
private void draw() {
if (movie != null) {
try {
if (visible) {
Canvas canvas = holder.lockCanvas();
canvas.save();
final float scaleFactorX = mDeviceWidth / (mAnimationWidth * 1.f); //608 is image width
final float scaleFactorY = mDeviceHeight / (mAnimationHeight * 1.f);
// Adjust size and position to fit animation on the screen
canvas.scale(scaleFactorX, scaleFactorY); // w,h Size of displaying Item
movie.draw(canvas, 0, 0); // position on x,y
canvas.restore();
holder.unlockCanvasAndPost(canvas);
movie.setTime((int) (System.currentTimeMillis() % movie.duration()));
handler.removeCallbacks(drawGIF);
handler.postDelayed(drawGIF, frameDuration);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
#Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
handler.post(drawGIF);
} else {
handler.removeCallbacks(drawGIF);
}
}
#Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(drawGIF);
}
}
}

Universal Image Loader: Get Original height and width onClick

I use Universal Image Loader to load images in a Jsoup parsed html. The <img> tags doesn't have a static position, they can appear anywhere in the Html element. And since I want them to appear in the positions where the <img> are, I can't give them an image view.
This is the class that I'm using to load the images
public class UILImageGetter implements Html.ImageGetter, View.OnClickListener{
Context c;
TextView conatiner;
UrlImageDownloader urlDrawable;
public UILImageGetter(View textView, Context context) {
this.c = context;
this.conatiner = (TextView) textView;
}
#Override
public Drawable getDrawable(String source) {
urlDrawable = new UrlImageDownloader(c.getResources(), source);
if (Build.VERSION.SDK_INT >= 21) {
urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb,null);
} else {
urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb);
}
ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
return urlDrawable;
}
#Override
public void onClick(View v) {
}
private class SimpleListener extends SimpleImageLoadingListener {
UrlImageDownloader mUrlImageDownloader;
public SimpleListener(UrlImageDownloader downloader) {
super();
mUrlImageDownloader= downloader;
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
int width = loadedImage.getWidth();
int height = loadedImage.getHeight();
int newWidth = width;
int newHeight = height;
if (width > conatiner.getWidth()) {
newWidth = conatiner.getWidth();
newHeight = (newWidth * height) / width;
}
if (view != null) {
view.getLayoutParams().width = newWidth;
view.getLayoutParams().height = newHeight;
}
Drawable result = new BitmapDrawable(c.getResources(), loadedImage);
result.setBounds(0, 0, newWidth, newHeight);
mUrlImageDownloader.setBounds(0, 0, newWidth, newHeight);
mUrlImageDownloader.mDrawable = result;
conatiner.setHeight((conatiner.getHeight() + result.getIntrinsicHeight()));
//conatiner.invalidate();
}
}
private class UrlImageDownloader extends BitmapDrawable {
public Drawable mDrawable;
public UrlImageDownloader(Resources resources, String filepath) {
super(resources, filepath);
mDrawable = new BitmapDrawable(resources, filepath);
}
#Override
public void draw(Canvas canvas) {
if (mDrawable != null) {
mDrawable.draw(canvas);
}
}
}
}
My problem is how to set OnclickListener on the images and get them to display (in a dialog) the original height and width when clicked.

How to apply matrix to customimageview in viewpager?

I have added custom imageview to viewpager, while setting matrix to canvas of customimageview , the customimageview doesn't drag with viepager movement
(like it is fixed in the same place). I couldn't realize what the problem.
//this is my simeple ImagePagerAdapter of viewpager
public class ImagePagerAdapter extends FragmentStatePagerAdapter {
private TabPageIndicator indicator;
private final int mSize;
private PageFragment pageFragment;
public ImagePagerAdapter(FragmentManager fm, int size) {
super(fm);
mSize = size;
}
#Override
public int getCount() {
return mSize;
}
#Override
public Fragment getItem(int position) {
PageMatrixFragment fragment = new PageMatrixFragment(position);
return fragment;
}
}
public class PageMatrixFragment extends Fragment {
int pos;
public PageMatrixFragment(int pos) {
this.pos = pos;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CustomImageView customImageView = new CustomImageView(getActivity());
if (pos % 2==0) {
customImageView.setBackgroundColor(Color.RED);
}else{
customImageView.setBackgroundColor(Color.BLUE);
}
return customImageView;
}
}
public class CustomImageView extends ImageView {
private Bitmap mMarker;
public CustomImageView(Context context) {
super(context);
init();
}
private void init() {
mMarker = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
protected void onDraw(Canvas c) {
super.onDraw(c);
Matrix matrix = new Matrix();
float[] values =
{1, 0, 50,
0, 1, 50,
0, 0, 1};
matrix.setValues(values);
//problems accure when I use this line it forces the movement of customimageview with viewpager
c.setMatrix(matrix);
Paint paint = new Paint();
c.drawBitmap(mMarker, 200, 200, paint);
}
}
The first screenshot shows the start state ,
when I commented matrix.setValues(values); line it works as should work like in 2_nd screenshot
while I need to apply matrix to canvas ,but it causes the result like it is in 3_rd screenshot ,it doesn't move with viewpager/

Make an custom view and extends it from other activities

i have a game which have a recycle process for each level , but it will be harder by the time,
can i make a view for common feature and extend it by each level activity??
any one have a solution ? or another idea ? ( i need each level in a separated activity )
that's what i made ...
public class Prev extends Activity{
private Bitmap image1;
private Bitmap image2;
private Bitmap image3;
private Bitmap image4;
private final int IMAGE_1_X = 50;
private final int IMAGE_2_X = 450;
private final int IMAGE_3_X = 50;
private final int IMAGE_4_X = 450;
private final int IMAGE_1_Y = 50;
private final int IMAGE_2_Y = 50;
private final int IMAGE_3_Y = 300;
private final int IMAGE_4_Y = 300;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Preview p = new Preview(this);
setContentView(p);
}
public void setImages(Bitmap image1, Bitmap image2, Bitmap image3, Bitmap image4)
{
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
this.image4 = image4;
}
public void setRawsNames(String sound_1, String sound_2, String sound_3, String sound_4)
{
this.sound_1 = sound_1;
this.sound_2 = sound_2;
this.sound_3 = sound_3;
this.sound_4 = sound_4;
}
public class Preview extends SurfaceView{
public Preview(Context context)
{
super(context);
}
#Override
public void onDraw(Canvas canvas)
{
canvas.drawBitmap(image1, IMAGE_1_X, IMAGE_1_Y,null);
canvas.drawBitmap(image2, IMAGE_2_X, IMAGE_2_Y,null);
canvas.drawBitmap(image3, IMAGE_3_X, IMAGE_3_Y,null);
canvas.drawBitmap(image4, IMAGE_4_X, IMAGE_4_Y,null);
}
}
}
here i will extend the upper class
public class MainActivity extends Prev {
private Bitmap image1;
private Bitmap image2;
private Bitmap image3;
private Bitmap image4;
public MainActivity()
{
preperImages();
setImages(image1, image2, image3, image4);
}
private void preperImages()
{
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.one);
image2 = BitmapFactory.decodeResource(getResources(), R.drawable.two);
image3 = BitmapFactory.decodeResource(getResources(), R.drawable.three);
image4 = BitmapFactory.decodeResource(getResources(), R.drawable.four);
}
}
Not sure what you exactly want to achieve. From what I understand, you want to reuse a view. For this, you can use include tag in your layout.

How to get Width and height of item in the drawable?

I am trying to declare and initialize a variable globally as final, this variable should holde thewidth and the height of an element in the drawable, how to achieve that.
I want smothing like the followinf "ofcourse it is not working, but it is roughly what i am trying to achieve:
private final int w = R.drawable.element.getwidth
You can define a function getDrawableWidth
private int getDrawableWidth(Resources resources, int id){
Bitmap bitmap = BitmapFactory.decodeResource(resources, id);
return bitmap.getWidth();
}
Then call it as below:
private final int w = getDrawableWidth(getResources(), R.drawable.element)
===Edit===
define a class to get global context(from here):
public class App extends Application{
private static Context mContext;
#Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
then change getDrawableWidth to another format:
private int getDrawableWidth(int id){
Bitmap bitmap = BitmapFactory.decodeResource(App.getContext().getResources(), id);
return bitmap.getWidth();
}
use it any where:
private final int w = getDrawableWidth(R.drawable.element)

Categories

Resources