I'm trying to simulate swapping two views (one goes up and ones go down) with Translate animation.
Basically, it swaps and re-swap again.
This is my activity.
LinearLayout topView, belowView;
TextView foo, bar;
int viewHeight;
boolean noSwap = true;
private static int ANIMATION_DURATION = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide);
topView = (LinearLayout) findViewById(R.id.top_view);
belowView = (LinearLayout) findViewById(R.id.below_view);
foo = (TextView) findViewById(R.id.foo);
bar = (TextView) findViewById(R.id.bar);
ImageButton btnSwitch = (ImageButton) findViewById(R.id.switch_btn);
ViewTreeObserver viewTreeObserver = foo.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
foo.getViewTreeObserver().addOnGlobalLayoutListener(this);
viewHeight = foo.getHeight();
foo.getLayoutParams();
}
});
}
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
if (noSwap) {
//Log.d("Swap Status", "Not Swapping yet ? " + true);
TranslateAnimation ta1 = new TranslateAnimation(0, 0, 0, viewHeight);
ta1.setDuration(ANIMATION_DURATION);
ta1.setFillAfter(true);
topView.startAnimation(ta1);
topView.bringToFront();
belowView.setY(0);
TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, -viewHeight);
ta2.setDuration(ANIMATION_DURATION);
ta2.setFillAfter(true);
belowView.startAnimation(ta2);
belowView.bringToFront();
noSwap = false;
} else {
//Log.d("Swap Status", "Swapped already ? " + true);
TranslateAnimation ta1 = new TranslateAnimation(0, 0, viewHeight, 0);
ta1.setDuration(ANIMATION_DURATION);
ta1.setFillAfter(true);
topView.startAnimation(ta1);
topView.bringToFront();
belowView.setY(0);
TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, viewHeight);
ta2.setDuration(ANIMATION_DURATION);
ta2.setFillAfter(true);
belowView.startAnimation(ta2);
belowView.bringToFront();
noSwap = true;
}
}
});
}
This is the layout.
<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="${packageName}.${activityClass}">
<LinearLayout
android:id="#+id/top_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/foo"
android:text="#string/foo"
android:textSize="30sp"
android:textColor="#android:color/holo_purple"
android:padding="10dp"
android:fontFamily="sans-serif-condensed"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="#+id/below_view"
android:layout_below="#+id/top_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/bar"
android:text="#string/bar"
android:textSize="30sp"
android:textColor="#android:color/holo_red_light"
android:padding="10dp"
android:fontFamily="sans-serif-condensed"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<ImageButton android:id="#+id/switch_btn"
style="?android:borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_import_export"
android:contentDescription="#string/swap"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Everything seems fine until I click the Switch button for third times when the top view is supposed to go down and below view is to go up. But the starting position of the below view is not correct. I've tried setting several positions to below view but still can't get it right.
I've made a screencast about it here.
I think I get it right. Here's the working class.
LinearLayout topView, belowView;
TextView foo, bar;
int viewHeight;
boolean noSwap = true;
private static int ANIMATION_DURATION = 300;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide);
topView = (LinearLayout) findViewById(R.id.top_view);
belowView = (LinearLayout) findViewById(R.id.below_view);
foo = (TextView) findViewById(R.id.foo);
bar = (TextView) findViewById(R.id.bar);
ImageButton btnSwitch = (ImageButton) findViewById(R.id.switch_btn);
ViewTreeObserver viewTreeObserver = foo.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
foo.getViewTreeObserver().addOnGlobalLayoutListener(this);
viewHeight = foo.getHeight();
foo.getLayoutParams();
}
});
}
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
if (noSwap) {
TranslateAnimation ta1 = new TranslateAnimation(0, 0, 0, viewHeight);
ta1.setDuration(ANIMATION_DURATION);
ta1.setFillAfter(true);
topView.startAnimation(ta1);
topView.bringToFront();
TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, -viewHeight);
ta2.setDuration(ANIMATION_DURATION);
ta2.setFillAfter(true);
belowView.startAnimation(ta2);
belowView.bringToFront();
noSwap = false;
} else {
TranslateAnimation ta1 = new TranslateAnimation(0, 0, viewHeight, 0);
ta1.setDuration(ANIMATION_DURATION);
ta1.setFillAfter(true);
topView.startAnimation(ta1);
topView.bringToFront();
TranslateAnimation ta2 = new TranslateAnimation(0, 0, -viewHeight, 0);
ta2.setDuration(ANIMATION_DURATION);
ta2.setFillAfter(true);
belowView.startAnimation(ta2);
belowView.bringToFront();
noSwap = true;
}
}
});
}
Related
I've followed this tutorial http://android-er.blogspot.co.id/2014/01/implement-drag-and-drop-movable.html with some modification. I want to show image on it and make the pop up draggable.
now it's work. but the problem is the image can not move to the corner of the screen. can anyone give me a solution about this issue? any suggestion would be appreciated
this my layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imgResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dismiss"
android:paddingTop="16dp"
android:text="close"/>
</LinearLayout>
and this my activity class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
ImageView imageView = (ImageView) popupView.findViewById(R.id.imgResult);
int imageResource = getResources().getIdentifier("#drawable/toy", null, getPackageName());
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
popupView.setOnTouchListener(new OnTouchListener() {
int orgX, orgY;
int offsetX, offsetY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
orgX = (int) event.getX();
orgY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
offsetX = (int)event.getRawX() - orgX;
offsetY = (int)event.getRawY() - orgY;
popupWindow.update(offsetX, offsetY, -1, -1, true);
break;
}
return true;
}});
}
});
}
}
I have a animation custom drawer menu class calling from 5 different activity , in 4 of them it works fine, but in one of them that i'm using FragmentActivity instead of Activity not working.
the behavior of menu in FragmentActivity is weird , in debug mode the code running well but it seems the drawer menu class have not UIthread, even when i'm using runOnUiThread.
I'm initialize the custom drawer class on onCreate() method of each Activity and in button click just call the DraverVisibility() method in Drawer class.
MainActivity class :
public class MainActivity extends FragmentActivity {
DraverMenu DraverMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DraverMenu = new DraverMenu(this,
findViewById(android.R.id.content),
findViewById(R.id.draver_l1),
findViewById(R.id.activity_main_app_name),
findViewById(R.id.activity_main_app_icon),
findViewById(R.id.draver_change_language));
...
#Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
DraverMenu.DraverVisibility();
return false;
}
class DraverMenu :
public class DraverMenu {
private final Activity A;
private final int LEFT_DRAVER_MARGIN = 220;
private final int TOP_DRAVER_MARGIN = 45;
private final int OPEN_DRAVER_DURATION = 300;
private final int CLOSE_DRAVER_DURATION = 200;
private float left_px;
private float top_px;
private LinearLayout draver;
private View content;
public DraverMenu(Activity a, View content, View draver,
final View name, View icon, View language) {
this.A = a;
this.draver = (LinearLayout) draver;
left_px = ...;
top_px = ...;
FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) draver.getLayoutParams();
// if api<
parm.gravity = Gravity.TOP;
parm.leftMargin = -(int) left_px;
parm.topMargin = (int) top_px;
draver.setLayoutParams(parm);
this.content = content;
...
}
public void DraverVisibility() {
int vis = draver.getVisibility();
if (vis == View.VISIBLE) {
TranslateAnimation movetoleft = new TranslateAnimation(0, -left_px, 0, 0);
movetoleft.setDuration(CLOSE_DRAVER_DURATION);
movetoleft.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams)
draver.getLayoutParams();
// if api<
parm.gravity = Gravity.TOP;
parm.leftMargin = -(int) left_px;
parm.topMargin = (int) top_px;
draver.setLayoutParams(parm);
draver.setVisibility(View.INVISIBLE);
draver.setAnimation(null);
draver.invalidate();
content.setAnimation(null);
content.invalidate();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
draver.startAnimation(movetoleft);
}
if (vis == View.INVISIBLE) {
InputMethodManager imm = (InputMethodManager) A.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(draver.getWindowToken(), 0);
TranslateAnimation movetoRight = new TranslateAnimation(0, left_px, 0, 0);
movetoRight.setDuration(OPEN_DRAVER_DURATION);
movetoRight.setFillEnabled(true);
movetoRight.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
draver.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) draver.getLayoutParams();
// if api<
parm.gravity = Gravity.TOP;
parm.leftMargin = 0; //-(int)left_px;
parm.topMargin = (int) top_px;
draver.setLayoutParams(parm);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
draver.startAnimation(movetoRight);
}
}
}
The XML of DrawerMenu is inclode in MainActivity XML
DrawerMenu XML :
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="match_parent"
android:background="#cc000000"
android:visibility="invisible"
android:id="#id/draver_l1"
android:layout_marginTop="30dp"
android:layout_gravity="center_vertical"
android:gravity="center">
<TextView
android:layout_width="130dp"
android:layout_height="130dp"
android:id="#+id/textView" android:background="#drawable/ic_launcher"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/draver_language_button_text"
android:id="#id/draver_change_language"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/draver_software_info"
android:id="#id/draver_button_2"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/draver_contact_us"
android:id="#id/draver_button_3"/>
<TextView
android:layout_width="130dp"
android:layout_height="130dp"
android:id="#+id/textView1" android:background="#drawable/barcodeqr"/>
</LinearLayout>
any suggestion ?
If I want to add a button in this class so that I can call the onclicklistener, how should I do it?
public class GameView extends View {
Path circle;
Paint cPaint;
Paint tPaint;
String z;
int i = 65, strt, arc, leftx, topy, rightx, bottomy, maxx, maxy;
boolean flag1, flag2, flag3;
double n1, n2;
int n, n3 = 180,n4,n5 = 90;
float f1 = 180, f2 = 90;
Button b1;
Random r = new Random();
RectF oval;
public GameView(Context context) {
super(context);
leftx = 0;
topy = 60;
rightx = 150;
bottomy = 120;
z = String.valueOf(Character.toChars(i));
cPaint = new Paint();
cPaint.setColor(Color.RED);
strt = 45;
arc = 315;
n1 = Math.random() * 600;
Log.d("random", z);
if (flag2 == false)
new DrawThread(this);
// cPaint.setStrokeWidth(2);
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
tPaint.setColor(Color.BLACK);
float scale = getResources().getDisplayMetrics().scaledDensity;
tPaint.setTextSize(20 * scale);
}
public void onSizeChanged(int w,int h,int oldh,int oldw) {
maxx = oldw;
maxy = oldh;
}
//#Override
protected void onDraw(Canvas canvas) {
// Drawing commands go here
oval = new RectF(leftx,topy,rightx,bottomy);
canvas.drawArc(oval, strt, arc, true, cPaint);
while (i < 90) {
canvas.drawText(String.valueOf(Character.toChars(i)),f1,f2, tPaint);
break;
}
}
}
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
add the above below setContentView
setContentView(R.layout.activity_new_game);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
You need to set the layout using setContentView and then initialize views.
Note you can findViewById of the current view hierarchy set to the activity
Edit:
Example: This is an example. I don't see why it won't work if you do similar as below.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".NewGame"
android:id="#+id/relative_layout"
>
<Button
android:id="#+id/prev_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/next_button"
android:layout_alignBottom="#+id/next_button"
android:layout_centerHorizontal="true"
android:text="stop_label" />
<Button
android:id="#+id/next_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/prev_button"
android:text="start_label" />
<RelativeLayout
android:layout_width="wrap_content"
android:id="#+id/rl"
android:layout_below="#+id/prev_button"
android:layout_height="wrap_content"
>
</RelativeLayout>
</RelativeLayout>
Activity
public class MainActivity extends Activity {
private Button btn1;
private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView mv = new MyView(this);
setContentView(R.layout.activity_main);
RelativeLayout layout= (RelativeLayout) findViewById(R.id.rl);
layout.addView(mv);
}
class MyView extends View
{
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.RED);
}
}
}
snapshot
You need to inflate your view before trying to find your relative layout. Try :
RelativeLayout layout;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gameview=new GameView(this);
setContentView(R.layout.activity_new_game);
layout = (RelativeLayout) findViewById(R.id.relative_layout);
//LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//view = mInflater.inflate(R.layout.activity_new_game, gameview, true);
layout.addView(gameview);
}
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
you can't call findViewById before setContentView. Move
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
after setContentView(R.layout.activity_new_game);
you didn't initialize layout in your code so First initialize layout after setContentView then add view to your layout
RelativeLayout layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
//Initialize game view
GameView gameview=new GameView(this);
//initialize layout
layout = (RelativeLayout) findViewById(R.id.relative_layout);
//adding game view to layout
layout.addView(gameview);
}
Edit:
initializing like this before setContentView
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
is wrong approach you must initialize views only after setContentView like the above code
I have an Activity with some elements like ImageView, Button, ToggleButton, ... . And a subview (LinearLayout) that contains an HorizontalScrollView of ImageView.
The subview is an element that I want to hide / show with an animation.
My animation works successfully. But when I touch a ToggleButton or I apply a Filter, the subview is reseted and back to its origin position.
I have deduce that the subview is replaced in its origin when an element of the view visually change.
But I don't understand why...
The Activity class
public class CameraActivity extends Activity implements PictureCallback
{
private ToggleButton flashButton;
private Button filterScrollButton;
private LinearLayout filterScrollView;
private LinearLayout filterScrollViewLayout;
private Boolean filtersIsOpened = false;
private ImageView filterImageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
flashButton = (ToggleButton)findViewById(R.id.button_flash);
filterScrollButton = (Button)findViewById(R.id.button_open_filters);
filterScrollView = (LinearLayout)findViewById(R.id.camera_scroll_filters);
filterScrollViewLayout = (LinearLayout)findViewById(R.id.camera_scroll_filters_layout);
}
...
private void initScrollFilters()
{
String[] filters = getResources().getStringArray(R.array.array_filters);
for (final String string : filters)
{
ImageView v = new ImageView(CameraActivity.this);
int imageFilterId = -1;
if (string != null && !string.isEmpty())
{
final int imageId = getResources().getIdentifier("#drawable/filter_" + string, null, getPackageName());
imageFilterId = getResources().getIdentifier("#drawable/filter_" + string, null, getPackageName());
v.setImageDrawable(getResources().getDrawable(imageId));
}
final int finalImageFilterId = imageFilterId;
v.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
Log.d(string + " filter image is touched");
CameraActivity.this.cameraManager.setImageFilter(finalImageFilterId); // Apply the new filter into filterImageView
}
});
filterScrollViewLayout.addView(v, 100, 100);
}
}
private void initListeners()
{
// Flash
flashButton.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// camera parameters is changed
}
});
// Filter scroll view
filterScrollButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("You click the filter scroll button men!!");
final float direction = (CameraActivity.this.filtersIsOpened) ? -1 : 1;
final float yDelta = -100;
final Animation animation = new TranslateAnimation(0, 0, 0, yDelta * direction);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation)
{
TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
anim.setDuration(1);
CameraActivity.this.filterScrollView.startAnimation(anim);
int top = (int) (CameraActivity.this.filterScrollView.getTop() + (yDelta * direction));
CameraActivity.this.filterScrollView.setTop(top);
}
});
animation.setDuration(500);
CameraActivity.this.filterScrollView.startAnimation(animation);
CameraActivity.this.filtersIsOpened = ! CameraActivity.this.filtersIsOpened;
}
});
}
...
}
The xml view
<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=".CameraActivity" >
<CameraPreview
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/camera_preview_filter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:rotation="90"
android:scaleType="center"
android:contentDescription="#string/content_desc_overlay" />
<LinearLayout
android:id="#+id/camera_scroll_filters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="-40dp"
android:paddingTop="40dp"
android:orientation="vertical"
android:clickable="false"
android:longClickable="false" >
<Button
android:id="#+id/button_open_filters"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="top|center_horizontal"
android:text="Filters" >
</Button>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF00FF00"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/camera_scroll_filters_layout"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:background="#FF000000" >
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="onCaptureClick"
android:text="#string/button_capture_text" />
</FrameLayout>
I'm basically trying to achieve drag&drop feature..
What i'm trying is that i provides sequence of images on the screen, if i click on any image available in images row that will be added to the Mains screen. But i'm getting problem that when i add new view into the Main Screen then all the other views also moved to top left corner.
Can you please tell me what is the problem...? Or kindly suggest me a tutorial or link where i can find solution.... or how to achieve this ?
I'm using Framelayout, So that i also achieve images overlapping...
This is the class in which all code is working:
public class drag extends Activity implements OnClickListener, OnTouchListener
{
ImageView img1;
Button btn,btn2;
FrameLayout layout;
LayoutParams params;
ImageView im , im2, im3 ,im4;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.vg);
layout.setDrawingCacheEnabled(true);
im = (ImageView)findViewById(R.id.img1);
im.setDrawingCacheEnabled(true);
im.setOnTouchListener(this);
im.setOnClickListener(this);
btn = (Button)findViewById(R.id.btn1);
btn.setDrawingCacheEnabled(true);
btn.setOnClickListener(this);
btn.setOnTouchListener(this);
btn2 = (Button)findViewById(R.id.btn2);
btn2.setDrawingCacheEnabled(true);
btn2.setOnClickListener(this);
btn2.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
im2 = new ImageView(drag.this);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
im2.setImageBitmap(bm);
im2.setOnTouchListener(drag.this);
im2.setOnClickListener(drag.this);
layout.addView(im2, params);
}
});
btn2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
saveImage(bm);
}
});
}
public void saveImage(Bitmap myBitmap)
{
MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, "mmsImage" , "mmsimage");
}
int l, t, r, b;
int oldLeft, oldTop;
PointF p, curr;
#Override
public boolean onTouch(View view, MotionEvent me)
{
if (me.getAction() == MotionEvent.ACTION_DOWN)
{
//status = START_DRAGGING;
Log.i("status"," AAA dOWN");
img1 = new ImageView(this);
view.setDrawingCacheEnabled(true);
Bitmap mmsImage = Bitmap.createBitmap(view.getDrawingCache());
img1.setImageBitmap(mmsImage);
img1.setOnTouchListener(drag.this);
img1.setOnClickListener(drag.this);
oldLeft = (int)view.getLeft();
oldTop = (int)view.getTop();
p = new PointF(me.getRawX(), me.getRawY());
}
if (me.getAction() == MotionEvent.ACTION_MOVE)
{
Log.i("status"," AAA draging");
int xDiff = (int)(me.getRawX() - p.x);
int yDiff = (int)(me.getRawY() - p.y);
p.x = me.getRawX();
p.y = me.getRawY();
l = view.getLeft();
t = view.getTop();
r = view.getRight();
b = view.getBottom();
view.layout(l + xDiff, t + yDiff , r + xDiff, b + yDiff);
}
if (me.getAction() == MotionEvent.ACTION_UP)
{
Log.i("status"," AAA UP");
//captureUserMove(view);
}
return false;
}
}
Here is the XML :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
You set params, but don't specify how to position the added view. Try this:
In onCreate()
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT | Gravity.TOP; // you need this for margins to work
In the click listener:
// your x and y where you want the new view
params.leftMargin = x;
params.topMargin = y;
Putting
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
inside
#Override
public void onClick(View v)
{
}
will work.