how to add zoom effect in imageview - android

I want to show a image in a activity . I successfully made it with imageview.But it has no zoom effect ie. I can't zoom the pic. now how can I add this zoom efect.
my .xml code is :
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/cat" />

use the lib https://github.com/chrisbanes/PhotoView
In code
ImageView mImageView;
PhotoViewAttacher mAttacher;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Any implementation of ImageView can be used!
mImageView = (ImageView) findViewById(R.id.iv_photo);
// Set the Drawable displayed
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
mImageView.setImageDrawable(bitmap);
// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
mAttacher = new PhotoViewAttacher(mImageView);
}
// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
attacher.update();

Related

Android how to drag edge/side to resize imageview by using picasso library

i manage to do a imageview that allow user drag edge/ side to resize the imageview, currently i select the imageview by using the picasso library, is it possible to implement the drag edge/side to resize the imageview by using picasso library or need to implement it by other library, any suggestion to solve this problem?
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.placeholder(R.drawable.placeholder) // optional
.error(R.drawable.error) // optional
.resize(450, 500) // optional
.rotate(90) // optional
.into(imageView);
}
}

ImageSwitcher image won't display onCreate()

I can't figure out why my ImageSwitcher won't display the image when the Activity first loads. I've set the image resource in the onCreate method but the image seems to be invisible until I start the animation. The animation should slide one image out and a second image in. When I click the button to start the animation, I can see the original image slide out from where it should be but I can't see it at the start. I've tried setting the image within the ViewFactory and just outside but it doesn't seem to work
Here's my imageSwitcher code:
mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
mImageSwitcher.setOnClickListener(mImageSwitcherClickListener);
mImageSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
ImageView view = new ImageView(getApplicationContext());
view.setScaleType(ImageView.ScaleType.CENTER);
view.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setImageResource(R.mipmap.image);
return view;
}
});
mImageSwitcher.setImageResource(R.mipmap.image);
Here is the layout for the Activity:
<ImageSwitcher
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageSwitcher"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:alpha=".5"
android:scaleX="4"
android:scaleY="4"
android:layout_marginBottom="150dp"
android:animateFirstView="true"/>
In tutorials it shows you have to use animation
try this
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
reference from this link

Semi transparent Instruction activity in android

I want to display instruction activity when user opens the app for the first time. I am using shared preference for that. Whatever I am doing so far is working. But I think my way of achieving this is not right.
What I am following:
I am drawing a transparent instruction image(with instructions) in photoshop.
Checking if user is opening that page for the first time(using shared preference).
Displaying that particular image in an activity with translucent theme
private void showFrontPageGuideIfFirstTime(){
if(!prefKeeper.getBoolean(PreferenceKey.FRONT_GUIDE)){
Intent intent = new Intent(this, ShowGuide.class);
intent.putExtra(BACKGROUND_KEY, R.drawable.front_page_png);
this.startActivity(intent);
prefKeeper.putBoolean(PreferenceKey.FRONT_GUIDE, true);
}
}
And my instruction page looks something like(made in photoshop):
The Instruction Image
But I think by this way it would not work in all smart phone screens.
Where am I wrong, and what would be the best way of doing this?
Implementation as below
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/framelayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.excogitation.example_mvptdd.MainActivity">
<!-- Include your layout here-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</FrameLayout>
In your activity.java
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Parent FrameLayout
frameLayout = (FrameLayout) findViewById(R.id.framelayout);
// Dynamically create a relativelayout which will be appended to framelayout
final RelativeLayout relativeLayout = new RelativeLayout(getApplicationContext());
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.setBackgroundColor(Color.DKGRAY);
relativeLayout.setAlpha(0.7f);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// remove the whole relativelayout on click
frameLayout.removeView(relativeLayout);
}
});
RelativeLayout.LayoutParams params;
// Place 1st 30x40 ImageView at (50,60) coordinates
params = new RelativeLayout.LayoutParams(100, 100);
params.leftMargin = 20;
params.topMargin = 50;
final ImageView imageView1 = new ImageView(getApplicationContext());
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_star));
// Add 1st imageview to relative layout
relativeLayout.addView(imageView1, params);
// Place 2nd 30x40 ImageView at (100,60) coordinates
params = new RelativeLayout.LayoutParams(120, 120);
params.leftMargin = 800;
params.topMargin = 450;
final ImageView imageView2 = new ImageView(getApplicationContext());
imageView2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_star));
// Add 2nd imageview to relative layout
relativeLayout.addView(imageView2, params);
// finally add it ot the framelayout
frameLayout.addView(relativeLayout);
}
}
Ofcourse you should modify this code with your own images and colors and interactions. Its just a simple working version that is better than loading a whole image upfront when you all you want is smaller helper images on a translucent background for the instructions.
Also you in this way you make things more Android-ish and editable. You can add more children to the relative layout like a textview to include instructions.
Screenshot on load of app and hence the relative layout as an overlay.
Screenshot on click/touch , the relative layout is removed.

Placing one image over another image in android

I have a imageButton in android which user clicks. Then after his click I want t0 show a tick or a cross image on the toip of it.
How is it possible?
<ImageButton android:layout_width="wrap_content"
android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
android:background="#drawable/rhino" android:layout_marginRight="20dp"></ImageButton>
put your ImageButton and the tick image in a FrameLayout and make the visbility of the Tick image "Invisible" . So when you click on the ImageButton then change the state of Tick Image to Visible.
Get a reference to your ImageButton and then use one of its setImage methods, such as setImageResource.
You can achieve the same using ImageView. Use ImageView
testimage = (ImageView) findViewById(R.id.imageview);
testimage.setOnClickListener(listener);
write the logic to set both type of image to imageview in onclick event
public OnClickListener listener=new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("..set image button..");
Drawable[] layers = new Drawable[2];
layers[0] = getResources().getDrawable(R.drawable.btn_call);
layers[1] = getResources().getDrawable(R.drawable.blue_unfocus);
System.out.println(layers[1]+"...Drawable..."+layers[0]);
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);
}
};
Thanks
Deepak

Change ImageView

I have set up an ImageView in main.xml, if I want to access it from my View class and make it visible = false, how can I do this programatically?
Thank you
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
theView = new GameView(this);
theView.setBackgroundResource(R.layout.main);
setContentView(theView);
For example, in your layout xml file, there is a imageview1
<ImageView
android:id="#+id/imageview1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
in your java src,
ImageView img=(ImageView)findViewById(R.id.imageview1);
img.setVisibility(View.GONE);
img.setVisibility(View.VISIBLE);
img.setVisibility(View.INVISIBLE);
you can google the difference between View.Gone and View.VISIBLE
First you should retrieve a link to this view object. Than set visibility property of this object to View.INVISIBLE
ImageView imageView = (ImageView)findViewById(R.id.image_view);
imageView.setVisibility(View.INVISIBLE);
Use this property in the xml of that Imageview
android:visibility="visible"
and change the visibility programatically like this on some particular event:
image.setVisibility(ImageView.GONE)
where image is the instance of that imageview received through findViewById()
ImageView myView = (ImageView) findViewById(R.id.myView);
myView.setVisibility (android.View.INVISIBLE);
http://developer.android.com/reference/android/view/View.html#INVISIBLE
http://developer.android.com/reference/android/view/View.html#findViewById%28int%29
Suppose you have a ImageView called imageView.
Now access the imageView like
imageView=(ImageView) findViewById(R.id.your_image_view);
Now when you are trying to hide the imageView just use imageView.setVisibility(View.INVISIBLE);
Hope this will help you

Categories

Resources