My activity displays images and i want to display two buttons when the user touch the screen, and to disappear these button on next touch.
My activity file ImageViewPager.java is as follows:
package com.pankajvatsa.testfeet;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class ImageViewPager extends Activity {
// Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.activity_image_view_pager);
mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);
mainLay.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
bWallpaperButton.setVisibility(View.VISIBLE);
bDownloadButton.setVisibility(View.VISIBLE);
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
bWallpaperButton.setVisibility(View.INVISIBLE);
bDownloadButton.setVisibility(View.INVISIBLE);
return true;
}
return true;
}
});
bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);
// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}
And my xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
<android.support.v4.view.ViewPager
android:id="#+id/image_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
</android.support.v4.view.ViewPager>
<Button
android:id="#+id/bSetWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/set_wallpaper"
android:visibility="gone" />
<Button
android:id="#+id/bSaveToGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/save_local"
android:visibility="gone" />
</RelativeLayout>
Here is an example using LinearLayout but should work fine with RelativeLayout as well:
Button btn1;
boolean gone = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
LinearLayout ln = (LinearLayout)findViewById(R.id.LinearLayout01);
ln.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
if(!gone){
btn1.setVisibility(View.GONE);
gone = true;
}else{
btn1.setVisibility(View.VISIBLE);
gone = false;
}
}
return true;
}
});
}
Well, it somehow worked on removing the setOnTouchListener() from layout and adding it on the ViewPager.
My Updated code(with other changes for differentiating tap and swipe) is:
package com.pankajvatsa.testfeet;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class ImageViewPager extends Activity {
// Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;
boolean gone = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.activity_image_view_pager);
bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);
mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);
// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
viewpager.setOnTouchListener(new View.OnTouchListener() {
private float pointX;
private float pointY;
private int tolerance = 50;
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_DOWN:
pointX = event.getX();
pointY = event.getY();
break;
case MotionEvent.ACTION_UP:
boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
if(sameX && sameY){
//The user "clicked" certain point in the screen or just returned to the same position an raised the finger
if(gone == false){
bWallpaperButton.setVisibility(View.GONE);
bDownloadButton.setVisibility(View.GONE);
gone = true;
}else{
bWallpaperButton.setVisibility(View.VISIBLE);
bDownloadButton.setVisibility(View.VISIBLE);
gone = false;
}
}
}
return false;
}
});
}
}
Related
I want to make android app like the following site.
https://sectional-anatomy.org/ct-abdomen/ or like https://radiopaedia.org/cases/squamous-cell-carcinoma-oral-cavity
There are multiple pictures and these are changing by scrolling.
I have done something like that with imageswitcher and onTouch event with action_down and action_up but it doesn't work because when I remove my finger then it goes to the first image it doesn't stay where I left.
Here is my code:
package com.radiology.radiologymenu.tomografi;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import com.radiology.radiologymenu.R;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class ImageChangeActivity extends AppCompatActivity {
private ImageSwitcher imageSwitcher;
private ImageView box;
// Size
private int frameHeight;
private int boxSize;
private int screenHeight;
// Position
private int boxY;
// Speed
private int boxSpeed;
// Score
private int score = 0;
// Initialize Class
private Handler handler = new Handler();
private Timer timer = new Timer();
// Status Check
private boolean action_flg = false;
private boolean start_flg = false;
private String pic;
private ArrayList<Integer> slidePictures;
private static final Integer[] image= {
R.drawable.sagittal_boyun_1, R.drawable.sagittal_boyun_2, R.drawable.sagittal_boyun_3, R.drawable.sagittal_boyun_4,
R.drawable.sagittal_boyun_5, R.drawable.sagittal_boyun_6, R.drawable.sagittal_boyun_7, R.drawable.sagittal_boyun_8,
R.drawable.sagittal_boyun_9, R.drawable.sagittal_boyun_10, R.drawable.sagittal_boyun_11,
R.drawable.sagittal_boyun_12, R.drawable.sagittal_boyun_13, R.drawable.sagittal_boyun_14, R.drawable.sagittal_boyun_15,
R.drawable.sagittal_boyun_16, R.drawable.sagittal_boyun_17, R.drawable.sagittal_boyun_18, R.drawable.sagittal_boyun_19,
R.drawable.sagittal_boyun_20, R.drawable.sagittal_boyun_21, R.drawable.sagittal_boyun_22, R.drawable.sagittal_boyun_23,
R.drawable.sagittal_boyun_24, R.drawable.sagittal_boyun_25, R.drawable.sagittal_boyun_26, R.drawable.sagittal_boyun_27,
R.drawable.sagittal_boyun_28, R.drawable.sagittal_boyun_29, R.drawable.sagittal_boyun_30, R.drawable.sagittal_boyun_31,
R.drawable.sagittal_boyun_32, R.drawable.sagittal_boyun_33, R.drawable.sagittal_boyun_34, R.drawable.sagittal_boyun_35,
R.drawable.sagittal_boyun_36, R.drawable.sagittal_boyun_37, R.drawable.sagittal_boyun_38, R.drawable.sagittal_boyun_39,
R.drawable.sagittal_boyun_40, R.drawable.sagittal_boyun_41, R.drawable.sagittal_boyun_42, R.drawable.sagittal_boyun_43,
R.drawable.sagittal_boyun_44, R.drawable.sagittal_boyun_45 };
int i = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_activity_main);
pic = getIntent().getExtras().getString("pic");
slidePictures = (ArrayList<Integer>) getIntent().getExtras().getSerializable("slidePictures");
getView2(pic,slidePictures);
}
public void getView2(String strings, ArrayList<Integer> slidePictures){
setContentView(R.layout.slide_activity_main);
TextView textView = (TextView) findViewById(R.id.resimText);
textView.setText(strings.toString());
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
box = (ImageView) findViewById(R.id.box);
// Get screen size.
WindowManager wm = getWindowManager();
Display disp = wm.getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
screenHeight = size.y;
boxSpeed = Math.round(screenHeight / 60); // 1280 / 60 = 21.333... => 21
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
FrameLayout.LayoutParams params = new ImageSwitcher.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
return imageView;
}
});
imageSwitcher.setImageResource(slidePictures.get(0));
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
public void changePos() {
// Move Box
if (action_flg == true) {
// Touching
boxY -= boxSpeed;
if (i<slidePictures.size()-1){
i++;
imageSwitcher.setImageResource(slidePictures.get(i));
}
} else {
// Releasing
boxY += boxSpeed;
if (i>0){
i--;
imageSwitcher.setImageResource(slidePictures.get(i));
}
}
// Check box position.
if (boxY < 0) boxY = 0;
if (boxY > frameHeight - boxSize) boxY = frameHeight - boxSize;
box.setY(boxY);
}
public boolean onTouchEvent(MotionEvent me) {
if (start_flg == false) {
start_flg = true;
// Why get frame height and box height here?
// Because the UI has not been set on the screen in OnCreate()!!
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
frameHeight = frame.getHeight();
boxY = (int)box.getY();
// The box is a square.(height and width are the same.)
boxSize = box.getHeight();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
changePos();
}
});
}
}, 0, 40);
} else {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
action_flg = false;
} else if (me.getAction() == MotionEvent.ACTION_UP) {
action_flg = true;
}
}
return true;
}
}
I am developing a photo editor app. Where I am giving an option of add text to image i.e writing some CAPTION or you can say TAG on that image. What I want is on "A"`s click, an Edit-Text should be added to the in image. Where can I insert name or any text.
What I want is describes in below image...
My code is below....
Second.java
package com.MyFirstApp.myfirstapp;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Random;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.devsmart.android.ui.HorizontalListView;
public class Second extends Activity {
TouchImageView img_to_be_zoomedH, img_to_be_zoomed_secondH,
img_to_be_zoomedV, img_to_be_zoomed_secondV;
ImageView img_back, img_save;
HorizontalListView HListView, HListViewFirst, HListViewColor;
/* Save Parent Layout After Editing */
RelativeLayout parentLayoutforImgSaving;
Bitmap bitmap_img, bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
img_to_be_zoomedH = (TouchImageView) findViewById(R.id.img_to_be_zoomedH);
img_to_be_zoomed_secondH = (TouchImageView) findViewById(R.id.img_to_be_zoomed_secondH);
img_to_be_zoomedV = (TouchImageView) findViewById(R.id.img_to_be_zoomedV);
img_to_be_zoomed_secondV = (TouchImageView) findViewById(R.id.img_to_be_zoomed_secondV);
img_back = (ImageView) findViewById(R.id.img_back_icon);
img_save = (ImageView) findViewById(R.id.img_save_icon);
HListView = (HorizontalListView) findViewById(R.id.horizontal_list_view);
HListViewFirst = (HorizontalListView) findViewById(R.id.horizontal_list_view_first);
HListViewColor = (HorizontalListView) findViewById(R.id.horizontal_list_view_color);
parentLayoutforImgSaving = (RelativeLayout) findViewById(R.id.imagelayout);
/* Top Back Icon */
img_back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
/* Save Image Icon */
img_save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
saveImgAfterEditing(parentLayoutforImgSaving);
}
});
/* Left Image Touch Event */
img_to_be_zoomedH
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_secondH.setZoom(img_to_be_zoomedH);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomedH.getScrollPosition();
img_to_be_zoomed_secondH.setScrollPosition(
1 - pointF_img1.x, pointF_img1.y);
}
});
/* Right Image touch event */
img_to_be_zoomed_secondH
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomedH.setZoom(img_to_be_zoomed_secondH);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed_secondH
.getScrollPosition();
img_to_be_zoomedH.setScrollPosition(1 - pointF_img1.x,
pointF_img1.y);
}
});
/* Top Image Touch Event */
img_to_be_zoomedV
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_secondV.setZoom(img_to_be_zoomedV);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomedV.getScrollPosition();
img_to_be_zoomed_secondV.setScrollPosition(
pointF_img1.x, 1 - pointF_img1.y);
}
});
/* Bottom Image touch event */
img_to_be_zoomed_secondV
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomedV.setZoom(img_to_be_zoomed_secondV);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed_secondV
.getScrollPosition();
img_to_be_zoomedV.setScrollPosition(pointF_img1.x,
1 - pointF_img1.y);
}
});
int[] HorizontalListImages = new int[] { R.drawable.icon_grid,
R.drawable.icon_text, R.drawable.icon_clip_art };
final int[] HorizontalListImagesFirst = new int[] {
R.drawable.icon_go_back, R.drawable.icon_horizontal_grid,
R.drawable.icon_vertical_grid };
/* Animation References */
final Animation slideUp = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_up);
final Animation slideDown = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_down);
/* Setting Adapter for Horizontal List Views */
HorizontalListViewAdapter horizontalListViewAdapter = new HorizontalListViewAdapter(
Second.this, HorizontalListImages);
HListView.setAdapter(horizontalListViewAdapter);
/* Horizontal List View Item Click Listener */
HListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switch (position) {
case 0:
HorizontalListViewAdapterFirst horizontalListViewAdapterFirst = new HorizontalListViewAdapterFirst(
Second.this, HorizontalListImagesFirst);
HListViewFirst.setAdapter(horizontalListViewAdapterFirst);
HListView.startAnimation(slideDown);
HListView.setVisibility(View.GONE);
HListViewFirst.startAnimation(slideUp);
HListViewFirst.setVisibility(View.VISIBLE);
break;
case 1:
/* Space for adding dynamic Edit-Text */
break;
default:
break;
}
}
});
HListViewFirst.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
/* HighLight Selected Option */
for (int i = 0; i < HListViewFirst.getChildCount(); i++) {
if (position == i) {
HListViewFirst.getChildAt(i).setBackgroundColor(
Color.GRAY);
} else {
HListViewFirst.getChildAt(i).setBackgroundColor(
Color.TRANSPARENT);
}
}
/* Get Device`s Height, Width */
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(
displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
switch (position) {
case 0:
HListViewFirst.startAnimation(slideDown);
HListViewFirst.setVisibility(View.GONE);
HListView.startAnimation(slideUp);
HListView.setVisibility(View.VISIBLE);
break;
case 1:
/* Setting Center Layout For Both Images */
RelativeLayout.LayoutParams ImagelayoutParams1 = new RelativeLayout.LayoutParams(
width, width);
ImagelayoutParams1.addRule(RelativeLayout.CENTER_IN_PARENT);
((RelativeLayout) findViewById(R.id.imagelayout))
.setLayoutParams(ImagelayoutParams1);
/* Setting Left ImageView width, height */
RelativeLayout.LayoutParams layoutParamsLeft = new RelativeLayout.LayoutParams(
width / 2, width);
layoutParamsLeft.setMargins(0, 0, 0, 0);
img_to_be_zoomedH.setLayoutParams(layoutParamsLeft);
img_to_be_zoomedH.setVisibility(view.VISIBLE);
bitmap_img = ((BitmapDrawable) img_to_be_zoomedH
.getDrawable()).getBitmap();
bitmap_img = flipImage(bitmap_img, 2);
img_to_be_zoomed_secondH.setImageBitmap(bitmap_img);
/* Setting Right ImageView width, height */
RelativeLayout.LayoutParams layoutParamsRight = new RelativeLayout.LayoutParams(
width / 2, width);
layoutParamsRight.setMargins(width / 2, 0, 0, 0);
img_to_be_zoomed_secondH.setLayoutParams(layoutParamsRight);
// img_to_be_zoomedH.invalidate();
img_to_be_zoomed_secondH.setVisibility(view.VISIBLE);
/* Hiding Vertical TouchIMageViews */
if ((img_to_be_zoomedV.getVisibility() == view.VISIBLE)
|| (img_to_be_zoomed_secondV.getVisibility() == view.VISIBLE)) {
img_to_be_zoomedV.setVisibility(view.GONE);
img_to_be_zoomed_secondV.setVisibility(view.GONE);
}
break;
case 2:
/* Setting Center Layout For Both Images */
RelativeLayout.LayoutParams ImagelayoutParams2 = new RelativeLayout.LayoutParams(
width, width);
ImagelayoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
((RelativeLayout) findViewById(R.id.imagelayout))
.setLayoutParams(ImagelayoutParams2);
/* Setting Top ImageView width, height */
RelativeLayout.LayoutParams layoutParamsTop = new RelativeLayout.LayoutParams(
width, width / 2);
layoutParamsTop.setMargins(0, 0, 0, 0);
img_to_be_zoomedV.setLayoutParams(layoutParamsTop);
img_to_be_zoomedV.setVisibility(view.VISIBLE);
bitmap_img = ((BitmapDrawable) img_to_be_zoomedV
.getDrawable()).getBitmap();
bitmap_img = flipImage(bitmap_img, 1);
img_to_be_zoomed_secondV.setImageBitmap(bitmap_img);
/* Setting Bottom ImageView width, height */
RelativeLayout.LayoutParams layoutParamsBottom = new RelativeLayout.LayoutParams(
width, width / 2);
layoutParamsBottom.setMargins(0, width / 2, 0, 0);
img_to_be_zoomed_secondV
.setLayoutParams(layoutParamsBottom);
// img_to_be_zoomedV.invalidate();
img_to_be_zoomed_secondV.setVisibility(view.VISIBLE);
/* Hiding Horizontal TouchIMageViews */
if ((img_to_be_zoomedH.getVisibility() == view.VISIBLE)
|| (img_to_be_zoomed_secondH.getVisibility() == view.VISIBLE)) {
img_to_be_zoomedH.setVisibility(view.GONE);
img_to_be_zoomed_secondH.setVisibility(view.GONE);
}
break;
default:
break;
}
}
});
/* Getting ImageURI from Gallery from Main Activity */
Uri selectedImgUri = getIntent().getData();
if (selectedImgUri != null) {
String[] selectedImgPath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImgUri,
selectedImgPath, null, null, null);
cursor.moveToFirst();
int indexCol = cursor.getColumnIndex(selectedImgPath[0]);
String imgPath = cursor.getString(indexCol);
cursor.close();
img_to_be_zoomedH.setImageBitmap(BitmapFactory.decodeFile(imgPath));
img_to_be_zoomedV.setImageBitmap(BitmapFactory.decodeFile(imgPath));
}
/* Getting ImageBitmap from Camera from Main Activity */
Intent intent_camera = getIntent();
Bitmap camera_img_bitmap = (Bitmap) intent_camera
.getParcelableExtra("BitmapImage");
if (camera_img_bitmap != null) {
img_to_be_zoomedH.setImageBitmap(camera_img_bitmap);
img_to_be_zoomedV.setImageBitmap(camera_img_bitmap);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/* Flip Image Function */
public Bitmap flipImage(Bitmap src, int type) {
Matrix matrix = new Matrix();
/* Flip vertically */
if (type == 1) {
matrix.preScale(1.0f, -1.0f);
/* Flip horizontally */
} else if (type == 2) {
matrix.preScale(-1.0f, 1.0f);
} else {
return null;
}
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
matrix, true);
}
/* Save Image Function */
private void saveImgAfterEditing(RelativeLayout perent) {
try {
View content = parentLayoutforImgSaving;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/" + getString(R.string.app_name));
if (!mFolder.exists()) {
mFolder.mkdir();
}
Calendar c = Calendar.getInstance();
String s = getString(R.string.app_name) + c.getTimeInMillis()
+ ".png";
File f = new File(mFolder.getAbsolutePath(), s);
FileOutputStream fos = null;
fos = new FileOutputStream(f);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
bitmap.recycle();
Toast.makeText(getBaseContext(), "Image Saved", 5000).show();
addImageGallery(f);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Failed To Save", 5000).show();
e.printStackTrace();
}
}
/* Save Image to Direct Gallery, No Need to Refresh */
private void addImageGallery(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
}
activity_second.java
<RelativeLayout 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="com.MyFirstApp.myfirstapp.Second" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" >
<ImageView
android:id="#+id/img_back_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="#string/img_back_icon"
android:src="#drawable/icon_back" />
<TextView
android:id="#+id/txtview_app_name_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/txtview_app_name_top" />
<ImageView
android:id="#+id/img_save_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/img_save_icon"
android:src="#drawable/icon_save" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/imagelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomedH"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="23dp"
android:src="#drawable/home" >
</com.MyFirstApp.myfirstapp.TouchImageView>
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomed_secondH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" >
</com.MyFirstApp.myfirstapp.TouchImageView>
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomedV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" >
</com.MyFirstApp.myfirstapp.TouchImageView>
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomed_secondV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" >
</com.MyFirstApp.myfirstapp.TouchImageView>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/horizontal_list_view"
android:layout_width="wrap_content"
android:layout_height="40dp" >
</com.devsmart.android.ui.HorizontalListView>
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/horizontal_list_view_first"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:choiceMode="singleChoice"
android:listSelector="#drawable/horizontal_list_view_selector"
android:visibility="gone" >
</com.devsmart.android.ui.HorizontalListView>
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/horizontal_list_view_color"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:visibility="gone" >
</com.devsmart.android.ui.HorizontalListView>
</RelativeLayout>
</RelativeLayout>
I want similar to below image...
Thanks in advance.
I am developing a photo-editor app. I have done horizontally and vertically flipping. Now, The problem is that When I choose horizontally flip option and do zoom then choose vertically flip option and do zoom and then I choose horizontally flip option again, Than left TouchImageView is misplaced, but right TouchImageView is in place. Below images explains much better.....
Step-1: Choose image from Gallery....
Step-2: Flip it horizontally....
Step-3: Zoom in horizontal flip....
Step-4: Flip it vertically....
Step-5: Zoom in vertical flip....
Step-6: Now back to horizontal flip....
Now, see step no 3 & 6. Step no.6 must be same as step no.3
What I have done is below.
Second.java
package com.MyFirstApp.myfirstapp;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.devsmart.android.ui.HorizontalListView;
public class Second extends Activity {
TouchImageView img_to_be_zoomedH, img_to_be_zoomed_secondH,
img_to_be_zoomedV, img_to_be_zoomed_secondV;
ImageView img_back, img_save;
HorizontalListView HListView, HListViewFirst;
/* Save Parent Layout After Editing */
RelativeLayout parentLayoutforImgSaving;
Bitmap bitmap_img, bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
img_to_be_zoomedH = (TouchImageView) findViewById(R.id.img_to_be_zoomedH);
img_to_be_zoomed_secondH = (TouchImageView) findViewById(R.id.img_to_be_zoomed_secondH);
img_to_be_zoomedV = (TouchImageView) findViewById(R.id.img_to_be_zoomedV);
img_to_be_zoomed_secondV = (TouchImageView) findViewById(R.id.img_to_be_zoomed_secondV);
img_back = (ImageView) findViewById(R.id.img_back_icon);
img_save = (ImageView) findViewById(R.id.img_save_icon);
HListView = (HorizontalListView) findViewById(R.id.horizontal_list_view);
HListViewFirst = (HorizontalListView) findViewById(R.id.horizontal_list_view_first);
parentLayoutforImgSaving = (RelativeLayout) findViewById(R.id.imagelayout);
/* Top Back Icon */
img_back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
/* Save Image Icon */
img_save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
saveImgAfterEditing(parentLayoutforImgSaving);
}
});
/* Left Image Touch Event */
img_to_be_zoomedH
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_secondH.setZoom(img_to_be_zoomedH);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomedH.getScrollPosition();
img_to_be_zoomed_secondH.setScrollPosition(
1 - pointF_img1.x, pointF_img1.y);
}
});
/* Right Image touch event */
img_to_be_zoomed_secondH
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomedH.setZoom(img_to_be_zoomed_secondH);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed_secondH
.getScrollPosition();
img_to_be_zoomedH.setScrollPosition(1 - pointF_img1.x,
pointF_img1.y);
}
});
/* Top Image Touch Event */
img_to_be_zoomedV
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_secondV.setZoom(img_to_be_zoomedV);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomedV.getScrollPosition();
img_to_be_zoomed_secondV.setScrollPosition(
pointF_img1.x, 1 - pointF_img1.y);
}
});
/* Bottom Image touch event */
img_to_be_zoomed_secondV
.setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomedV.setZoom(img_to_be_zoomed_secondV);
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed_secondV
.getScrollPosition();
img_to_be_zoomedV.setScrollPosition(pointF_img1.x,
1 - pointF_img1.y);
}
});
int[] HorizontalListImages = new int[] { R.drawable.icon_grid,
R.drawable.icon_text, R.drawable.icon_clip_art };
final int[] HorizontalListImagesFirst = new int[] {
R.drawable.icon_go_back, R.drawable.icon_horizontal_grid,
R.drawable.icon_vertical_grid };
/* Animation References */
final Animation slideUp = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_up);
final Animation slideDown = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_down);
/* Setting Adapter for Horizontal List Views */
HorizontalListViewAdapter horizontalListViewAdapter = new HorizontalListViewAdapter(
Second.this, HorizontalListImages);
HListView.setAdapter(horizontalListViewAdapter);
/* Horizontal List View Item Click Listener */
HListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switch (position) {
case 0:
HorizontalListViewAdapterFirst horizontalListViewAdapterFirst = new HorizontalListViewAdapterFirst(
Second.this, HorizontalListImagesFirst);
HListViewFirst.setAdapter(horizontalListViewAdapterFirst);
HListView.startAnimation(slideDown);
HListView.setVisibility(View.GONE);
HListViewFirst.startAnimation(slideUp);
HListViewFirst.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
});
HListViewFirst.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
/* HighLight Selected Option */
for (int i = 0; i < HListViewFirst.getChildCount(); i++) {
if (position == i) {
HListViewFirst.getChildAt(i).setBackgroundColor(
Color.GRAY);
} else {
HListViewFirst.getChildAt(i).setBackgroundColor(
Color.TRANSPARENT);
}
}
/* Get Device`s Height, Width */
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(
displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
switch (position) {
case 0:
HListViewFirst.startAnimation(slideDown);
HListViewFirst.setVisibility(View.GONE);
HListView.startAnimation(slideUp);
HListView.setVisibility(View.VISIBLE);
break;
case 1:
/* Setting Center Layout For Both Images */
RelativeLayout.LayoutParams ImagelayoutParams1 = new RelativeLayout.LayoutParams(
width, width);
ImagelayoutParams1.addRule(RelativeLayout.CENTER_IN_PARENT);
((RelativeLayout) findViewById(R.id.imagelayout))
.setLayoutParams(ImagelayoutParams1);
/* Setting Left ImageView width, height */
RelativeLayout.LayoutParams layoutParamsLeft = new RelativeLayout.LayoutParams(
width / 2, width);
layoutParamsLeft.setMargins(0, 0, 0, 0);
img_to_be_zoomedH.setLayoutParams(layoutParamsLeft);
img_to_be_zoomedH.setVisibility(view.VISIBLE);
bitmap_img = ((BitmapDrawable) img_to_be_zoomedH
.getDrawable()).getBitmap();
bitmap_img = flipImage(bitmap_img, 2);
img_to_be_zoomed_secondH.setImageBitmap(bitmap_img);
/* Setting Right ImageView width, height */
RelativeLayout.LayoutParams layoutParamsRight = new RelativeLayout.LayoutParams(
width / 2, width);
layoutParamsRight.setMargins(width / 2, 0, 0, 0);
img_to_be_zoomed_secondH.setLayoutParams(layoutParamsRight);
// img_to_be_zoomedH.invalidate();
img_to_be_zoomed_secondH.setVisibility(view.VISIBLE);
/* Hiding Vertical TouchIMageViews */
if ((img_to_be_zoomedV.getVisibility() == view.VISIBLE)
|| (img_to_be_zoomed_secondV.getVisibility() == view.VISIBLE)) {
img_to_be_zoomedV.setVisibility(view.GONE);
img_to_be_zoomed_secondV.setVisibility(view.GONE);
}
break;
case 2:
/* Setting Center Layout For Both Images */
RelativeLayout.LayoutParams ImagelayoutParams2 = new RelativeLayout.LayoutParams(
width, width);
ImagelayoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
((RelativeLayout) findViewById(R.id.imagelayout))
.setLayoutParams(ImagelayoutParams2);
/* Setting Top ImageView width, height */
RelativeLayout.LayoutParams layoutParamsTop = new RelativeLayout.LayoutParams(
width, width / 2);
layoutParamsTop.setMargins(0, 0, 0, 0);
img_to_be_zoomedV.setLayoutParams(layoutParamsTop);
img_to_be_zoomedV.setVisibility(view.VISIBLE);
bitmap_img = ((BitmapDrawable) img_to_be_zoomedV
.getDrawable()).getBitmap();
bitmap_img = flipImage(bitmap_img, 1);
img_to_be_zoomed_secondV.setImageBitmap(bitmap_img);
/* Setting Bottom ImageView width, height */
RelativeLayout.LayoutParams layoutParamsBottom = new RelativeLayout.LayoutParams(
width, width / 2);
layoutParamsBottom.setMargins(0, width / 2, 0, 0);
img_to_be_zoomed_secondV
.setLayoutParams(layoutParamsBottom);
// img_to_be_zoomedV.invalidate();
img_to_be_zoomed_secondV.setVisibility(view.VISIBLE);
/* Hiding Horizontal TouchIMageViews */
if ((img_to_be_zoomedH.getVisibility() == view.VISIBLE)
|| (img_to_be_zoomed_secondH.getVisibility() == view.VISIBLE)) {
img_to_be_zoomedH.setVisibility(view.GONE);
img_to_be_zoomed_secondH.setVisibility(view.GONE);
}
break;
default:
break;
}
}
});
/* Getting ImageURI from Gallery from Main Activity */
Uri selectedImgUri = getIntent().getData();
if (selectedImgUri != null) {
String[] selectedImgPath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImgUri,
selectedImgPath, null, null, null);
cursor.moveToFirst();
int indexCol = cursor.getColumnIndex(selectedImgPath[0]);
String imgPath = cursor.getString(indexCol);
cursor.close();
img_to_be_zoomedH.setImageBitmap(BitmapFactory.decodeFile(imgPath));
img_to_be_zoomedV.setImageBitmap(BitmapFactory.decodeFile(imgPath));
}
/* Getting ImageBitmap from Camera from Main Activity */
Intent intent_camera = getIntent();
Bitmap camera_img_bitmap = (Bitmap) intent_camera
.getParcelableExtra("BitmapImage");
if (camera_img_bitmap != null) {
img_to_be_zoomedH.setImageBitmap(camera_img_bitmap);
img_to_be_zoomedV.setImageBitmap(camera_img_bitmap);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/* Flip Image Function */
public Bitmap flipImage(Bitmap src, int type) {
Matrix matrix = new Matrix();
/* Flip vertically */
if (type == 1) {
matrix.preScale(1.0f, -1.0f);
/* Flip horizontally */
} else if (type == 2) {
matrix.preScale(-1.0f, 1.0f);
} else {
return null;
}
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
matrix, true);
}
/* Save Image Function */
private void saveImgAfterEditing(RelativeLayout perent) {
try {
View content = parentLayoutforImgSaving;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/" + getString(R.string.app_name));
if (!mFolder.exists()) {
mFolder.mkdir();
}
Calendar c = Calendar.getInstance();
String s = getString(R.string.app_name) + c.getTimeInMillis()
+ ".png";
File f = new File(mFolder.getAbsolutePath(), s);
FileOutputStream fos = null;
fos = new FileOutputStream(f);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
bitmap.recycle();
Toast.makeText(getBaseContext(), "Image Saved", 5000).show();
addImageGallery(f);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Failed To Save", 5000).show();
e.printStackTrace();
}
}
/* Save Image to Direct Gallery, No Need to Refresh */
private void addImageGallery(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
}
activity_second.xml
<RelativeLayout 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="com.MyFirstApp.myfirstapp.Second" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" >
<ImageView
android:id="#+id/img_back_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="#string/img_back_icon"
android:src="#drawable/icon_back" />
<TextView
android:id="#+id/txtview_app_name_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/txtview_app_name_top" />
<ImageView
android:id="#+id/img_save_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/img_save_icon"
android:src="#drawable/icon_save" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/imagelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomedH"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="23dp"
android:src="#drawable/home" >
</com.MyFirstApp.myfirstapp.TouchImageView>
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomed_secondH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" >
</com.MyFirstApp.myfirstapp.TouchImageView>
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomedV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" >
</com.MyFirstApp.myfirstapp.TouchImageView>
<com.MyFirstApp.myfirstapp.TouchImageView
android:id="#+id/img_to_be_zoomed_secondV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" >
</com.MyFirstApp.myfirstapp.TouchImageView>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/horizontal_list_view"
android:layout_width="wrap_content"
android:layout_height="40dp" >
</com.devsmart.android.ui.HorizontalListView>
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/horizontal_list_view_first"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:choiceMode="singleChoice"
android:listSelector="#drawable/horizontal_list_view_selector"
android:visibility="gone" >
</com.devsmart.android.ui.HorizontalListView>
</RelativeLayout>
</RelativeLayout>
Thanks in advance.
I currently have implemented an ImageSwitcher in my app, which slides through an array of pictures. Now I want to have a ProgressBar under the picture which shows on which position we are in the collection so the user gets a feeling of how much pictures are left.
Is this possible? Can I have some advices on how I might implement this?
Picture:
progress
[--|--------]
My ImageSwticher:
package com.example.cfaslides;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ProgressBar;
import android.widget.ViewSwitcher;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.view.Window;
import android.content.Context;
public class l2_AltInvestActivity extends Activity implements ViewFactory {
ImageSwitcher imageSwitcher;
Integer[] imageList = {
R.drawable.av_01,
R.drawable.av_02,
R.drawable.av_03,
R.drawable.av_04,
R.drawable.av_05
};
int curIndex=0;
int maxIndex = 4; //# imageList -1
int downX, upX;
private Animation mIn1, mOut1, mIn2, mOut2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slider);
final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressB);
mProgressBar.setProgress(0);
mProgressBar.setMax(maxIndex);
mProgressBar.setVisibility(View.VISIBLE);
mIn1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_in_left);
mOut1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_out_right);
mIn2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_in_right);
mOut2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_out_left);
AnimationListener mAnimListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
// the in animation has ended so update the ProgressBar with the new
// progress
mProgressBar.setProgress(curIndex); // I don't know your progress?!?
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
// rest of the callbacks
};
//set this listener for the both of the in animations
mIn1.setAnimationListener(mAnimListener);
mIn2.setAnimationListener(mAnimListener);
// rest of the onCreate method
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
imageSwitcher.setImageResource(imageList[curIndex]);
imageSwitcher.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX();
Log.i("event.getX()", " downX " + downX);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX();
Log.i("event.getX()", " upX " + downX);
if (upX - downX > 100) {
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_out_right));
//curIndex current image index in array viewed by user
curIndex--;
if (curIndex < 0) {
curIndex = maxIndex; //maximum
}
//imageList :-image list array
imageSwitcher.setImageResource(imageList[curIndex]);
//GalleryActivity.this.setTitle(curIndex);
}
else if (downX -upX > -100) {
curIndex++;
if (curIndex > maxIndex) {
curIndex = 0;
}
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_in_right));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_out_left));
imageSwitcher.setImageResource(imageList[curIndex]);
//GalleryActivity.this.setTitle(curIndex);
}
return true;
}
return false;
}
});
} //END onCreate
#Override
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
i.setBackgroundColor(0xFF000000);
return i;
} //END makeView
} // END Class
Slider:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="#+id/progressB"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageSwitcher android:id="#+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
</ImageSwitcher>
</RelativeLayout>
now i like to have an progress bar under the picture which shows on
which position we are in the collection. so that someone gets a
feeling how much pictures are left.
Place a ProgressBar in your R.layout.slider layout file
Make the four different Animations that you used for the ImageSwitcher's transitions as fields in your Activity and also set an AnimationListener for each of them:
//...
private Animation mIn1, mOut1, mIn2, mOut2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slider);
final ProgressBar = (ProgressBar) findViewById(R.id.theIdOfTheProgressBar);
mIn1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_in_left);
mOut1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_out_right);
mIn2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_in_right);
mOut2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_out_left);
AnimationListener mAnimListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
// the in animation has ended so update the ProgressBar with the new
// progress
mProgressBar.setProgress(curIndex); // I don't know your progress?!?
}
// rest of the callbacks
});
//set this listener for the both of the in animations
mIn1.setAnimationListener(mAnimListener);
mIn2.setAnimationListener(mAnimListener);
// rest of the onCreate method
In the onTouch method update the ImageSwitcher with the proper animations(from mIn1, mOut1, mIn2, mOut2)
I have developed an Application , which uses screen as a Slate and finger as a Chalk, which is working properly. But I want to use different color types for chalk.
Here is my Code:
MyDemo.java
package com.example.mydemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyDemo extends Activity {
private LinearLayout root;
private Button btnReset;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_demo);
root = (LinearLayout) findViewById(R.id.root);
btnReset = (Button) findViewById(R.id.reset);
final MyImageView view = new MyImageView(this);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
root.addView(view);
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.reset();
}
});
}
}
MyImageView.java
package com.example.mydemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MyImageView extends ImageView implements View.OnTouchListener {
private int id = -1;
private Path path;
private List<Path> paths = new ArrayList<Path>();
private List<PointF> points = new ArrayList<PointF>();
boolean multiTouch = false;
public MyImageView(Context context) {
super(context);
this.setOnTouchListener(this);
}
public void reset() {
paths.clear();
points.clear();
path = null;
id = -1;
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = createPen(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
for (Path path : paths) {
canvas.drawPath(path, paint);
}
for (int i = 0; i < points.size(); i++) {
PointF p = points.get(i);
canvas.drawText("" + p.x, p.y, i, createPen(Color.WHITE));
}
}
private PointF copy(PointF p) {
PointF copy = new PointF();
copy.set(p);
return copy;
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
multiTouch = false;
id = event.getPointerId(0);
PointF p = getPoint(event, id);
path = new Path();
path.moveTo(p.x, p.y);
paths.add(path);
points.add(copy(p));
break;
case MotionEvent.ACTION_POINTER_DOWN:
multiTouch = true;
for (int i = 0; i < event.getPointerCount(); i++) {
int tId = event.getPointerId(i);
if (tId != id) {
points.add(getPoint(event,i));
}
}
break;
case MotionEvent.ACTION_MOVE:
if (!multiTouch) {
p =getPoint(event, id);
path.lineTo(p.x, p.y);
}
break;
}
invalidate();
return true;
}
private PointF getPoint(MotionEvent event, int i) {
int index = 0;
return new PointF(event.getX(index), event.getY(index));
}
private Paint createPen(int color) {
Paint pen = new Paint();
pen.setColor(color);
float width = 3;
pen.setStrokeWidth(width);
return pen;
}
}
activity_my_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/root" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="#+id/reset" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
/>
</LinearLayout>
Can any one tell me what should be added or changed in my code so that I can use different colors for Chalk?
You can take the sample in your android folder's..
For the color picker go to in this folder:
android/samples/android-YOURS_VERSION/ApiDemos
Use this, review this code. than your problem will be solve
package com.example.changecolor;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity
implements View.OnClickListener, UberColorPickerDialog.OnColorChangedListener {
private int mColor = 0xFFFF0000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Write the version number
PackageManager pm = getPackageManager();
String versionName = "";
try {
PackageInfo pi = pm.getPackageInfo("com.keithwiley.android.ubercolorpickerdemo", 0);
versionName = pi.versionName;
}
catch (Exception e) {
}
TextView textView = (TextView) findViewById(R.id.version);
textView.setText(versionName);
//Initialize the sample
((LinearLayout) findViewById(R.id.LinearLayout)).setBackgroundColor(mColor);
float hsv[] = new float[3];
Color.colorToHSV(mColor, hsv);
if (UberColorPickerDialog.isGray(mColor))
hsv[1] = 0;
if (hsv[2] < .5)
((TextView) findViewById(R.id.sample)).setTextColor(Color.WHITE);
else ((TextView) findViewById(R.id.sample)).setTextColor(Color.BLACK);
//Set up the buttons
Button button = (Button) findViewById(R.id.colorPickerWithTitle);
button.setOnClickListener(this);
button = (Button) findViewById(R.id.colorPickerWithToast);
button.setOnClickListener(this);
}
protected void onPause() {
super.onPause();
}
protected void onResume() {
super.onResume();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v) {
if (v.getId() == R.id.colorPickerWithTitle)
new UberColorPickerDialog(this, this, mColor, true).show();
if (v.getId() == R.id.colorPickerWithToast)
new UberColorPickerDialog(this, this, mColor, false).show();
}
public void colorChanged(int color) {
((LinearLayout) findViewById(R.id.LinearLayout)).setBackgroundColor(mColor=color);
float hsv[] = new float[3];
Color.colorToHSV(mColor, hsv);
//if (UberColorPickerDialog.isGray(mColor))
// hsv[1] = 0;
if (hsv[2] < .5)
((TextView) findViewById(R.id.sample)).setTextColor(Color.WHITE);
else ((TextView) findViewById(R.id.sample)).setTextColor(Color.BLACK);
}
}
And use the color picker class