2017-02-21 8 views
0

У меня есть собственный макет (swipableLayout). Я динамически меняю свои поля на MotionEvents. Когда я устанавливаю любой ImageView или любой ImageButton с любым методом setImage (Bitmap, Drawable, Resource) из текущей активности в любое время, динамически управляемые поля возвращаются к их начальным значениям. Я предполагаю, что это что-то внутреннее с setImageXXX, которое каким-то образом влияет на все макеты.Android - Динамически измененные поля макета сбрасываются при вызове любого метода setImage на любое изображение из текущей активности

Я не могу найти связанную с этим проблему в Интернете.

GIF, показывающий вопрос:

http://giphy.com/gifs/rething-xTiN0BFP4kOzh76944

код XML:

Также протестирована с регулярными ImageViews

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.65" 
      android:layout_marginBottom="25dp"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="0.59" 
       android:id="@+id/LinearLayout" 
       android:weightSum="1" 
       android:layout_margin="10dp" 
       android:background="@drawable/image_edge_up"> 

       <pl.droidsonroids.gif.GifImageView 
        android:layout_width="fill_parent" 
        android:layout_height="300dp" 
        android:id="@+id/imageView9" 
        android:src="@drawable/preload_anim" 

        android:background="@drawable/image_edge_up" 
        android:scaleType="centerCrop" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:text="Name, age [gender] - breed " 
        android:id="@+id/textView13" 
        android:layout_weight="0.3" 
        android:background="#ffffff" 
        android:paddingLeft="10dp" 
        android:gravity="bottom|left" 
        android:textSize="18dp" 
        android:textColor="@color/black" 
        android:textStyle="bold" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:text="Park /zone, City" 
        android:id="@+id/textView6" 
        android:layout_weight="0.7" 
        android:textSize="18dp" 
        android:background="@drawable/image_edge_up" 
        android:backgroundTintMode="src_atop" 
        android:textColor="@color/black" 
        android:paddingLeft="10dp" /> 


      </LinearLayout> 


      <com.mindfunk.rething.utils.swipableLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="0.59" 
       android:id="@+id/swipableLayout" 
       android:weightSum="1" 
       android:layout_margin="10dp" 
       android:background="@drawable/image_edge_up" 
       android:visibility="visible"> 
       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="300dp"> 

        <pl.droidsonroids.gif.GifImageView 
         android:layout_width="match_parent" 
         android:layout_height="300dp" 
         android:id="@+id/imageView8" 


         android:src="@drawable/preload_anim" 
         android:background="@drawable/image_edge_up" 
         android:scaleType="centerCrop" 
         android:adjustViewBounds="false" 
         android:visibility="visible" 
         android:nestedScrollingEnabled="false" /> 

        <ImageView 
         android:layout_width="100dp" 
         android:layout_height="100dp" 
         android:layout_margin="10dp" 
         android:id="@+id/cross" 
         android:layout_gravity="right" 
         android:src="@drawable/cross" 
         android:tint="#ff0000" 
         android:alpha="0" /> 

        <ImageView 
         android:layout_width="100dp" 
         android:layout_height="100dp" 
         android:layout_margin="10dp" 
         android:id="@+id/tick" 
         android:layout_gravity="left" 
         android:src="@drawable/tick" 
         android:scaleType="fitCenter" 
         android:alpha="0" 
         android:tint="#00bb09" /> 

       </FrameLayout> 


       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:text="Name, age [gender]" 
        android:textSize="18dp" 
        android:id="@+id/textView3" 
        android:layout_weight="0.3" 
        android:background="#ffffff" 
        android:paddingLeft="10dp" 
        android:gravity="bottom|left" 
        android:textColor="@color/black" 
        android:textStyle="bold" 
        android:typeface="normal" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:text="Park /zone, City" 
        android:textSize="18dp" 
        android:id="@+id/textView4" 
        android:layout_weight="0.7" 
        android:background="@drawable/image_edge_up" 
        android:backgroundTintMode="src_atop" 
        android:textColor="@color/black" 
        android:typeface="normal" 
        android:paddingLeft="10dp" /> 

      </com.mindfunk.rething.utils.swipableLayout> 




     </RelativeLayout> 

Мой swipable расположение:

public class swipableLayout extends LinearLayout { 

private boolean swiping = false; 

public static float lastTouchx =0; 
public static float lastTouchy =0; 
public static float lastPosx =0; 
public static float lastPosy =0; 
private float initialTouchx =0; 
private float initialTouchy =0; 
private int initialPositionx=0; 
private int initialPositiony=0; 
private int height; 
private int width; 

public swipableLayout(Context context){ 

    super(context); 
} 

public swipableLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 

} 

public swipableLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

} 



@Override 
public boolean onTouchEvent(MotionEvent ev) { 

    final int action = MotionEventCompat.getActionMasked(ev); 



    switch(action){ 

     case MotionEvent.ACTION_DOWN:{ 
      final int pointerIndex = MotionEventCompat.getActionIndex(ev); 

      lastTouchx = ev.getRawX(); 
      lastTouchy = ev.getRawY(); 
      initialTouchx = lastTouchx; 
      initialTouchy = lastTouchy; 


      Log.d("SwipableLayout", "MotionEvent.ACTION_DOWN"); 
      LinearLayout swipableLayout = (LinearLayout) findViewById(R.id.swipableLayout); 
      height = swipableLayout.getHeight(); 
      width = swipableLayout.getWidth(); 

      initialPositionx = swipableLayout.getLeft(); 
      initialPositiony = swipableLayout.getTop(); 




      break; 

     } 
     case MotionEvent.ACTION_MOVE:{ 

      final int pointerIndex = MotionEventCompat.getActionIndex(ev); 

      LinearLayout swipableLayout = (LinearLayout) findViewById(R.id.swipableLayout); 

      final float diffX = lastTouchx - ev.getRawX();; 
      final float diffY = lastTouchy - ev.getRawY();; 
      /* final float diffX = lastTouchx - MotionEventCompat.getX(ev, pointerIndex); 
      final float diffY = lastTouchy - MotionEventCompat.getY(ev, pointerIndex);*/ 
      Log.d("layoutInfo", "[" 
        +String.valueOf(swipableLayout.getTop())+"," 
        +String.valueOf(swipableLayout.getLeft())+"," 
        +String.valueOf(swipableLayout.getBottom())+"," 
        +String.valueOf(swipableLayout.getRight())+"]" 

      ); 

      lastTouchx = swipableLayout.getLeft(); 
      lastTouchy = swipableLayout.getTop(); 

      lastTouchx = ev.getRawX(); 
      lastTouchy = ev.getRawY(); 

      float correction = (swipableLayout.getLeft() + swipableLayout.getRight())/2 - width/2f; 

      if(correction>0){ 

       swipeActivity.setTickAlpha(correction/width); 



      } 
      else swipeActivity.setCrossAlpha(-correction/width); 


      /* Log.d("correction/width/touchx", String.valueOf(lastTouchx)); 
      Log.d("correction/width/width", String.valueOf(width)); 
      Log.d("correction/width/2f", String.valueOf(correction));*/ 


      int newPosx = Math.round ((swipableLayout.getLeft() - (int) diffX)); 
      int newPosy = Math.round ((swipableLayout.getTop() - (int) diffY)); 

      swipableLayout.setLeft(newPosx); 
      swipableLayout.setTop(newPosy); 
      swipableLayout.setRight(newPosx+width); 
      swipableLayout.setBottom(newPosy+height); 
      swipableLayout.setRotation(swipableLayout.getLeft()/20); 


      break; 


     } 
     case MotionEvent.ACTION_UP:{ 

      swipeActivity.setCrossAlpha(0.0f); 
      swipeActivity.setTickAlpha(0.0f); 

      Log.d("SwipableLayout", "MotionEvent.ACTION_UP"); 

      if(Math.abs(initialTouchx - lastTouchx) > 0){// swipe 
      LinearLayout swipableLayout = (LinearLayout) findViewById(R.id.swipableLayout); 

      swipableLayout.setLeft(initialPositionx); 
      swipableLayout.setRight(initialPositionx + width); 
      swipableLayout.setTop(initialPositiony); 
       swipableLayout.setBottom(initialPositiony+height); 

      if(Math.abs(swipableLayout.getRotation()) >18) 
      swipeActivity.instance.moveProfiles(); 


       swipableLayout.setRotation(0.0f); 
      } 
      else { // click 
       swipeActivity.gotoPetViewer(); 
      } 

      break; 

     } 
    } 

    return true; 
} 
} 

Мой AsyncTask:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 

     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 


    protected void onPostExecute(Bitmap result) { 

// HERE IS THE ISSUE! 
     bmImage.setImageBitmap(result); //<- WHEN I SET THIS BITMAP, THE UPPER 
//swipableLayout goes to the initial position 

     imageView9.setTag("adios"); 
    } 
} 

задерживая Log.d 100 мс после того, как изображение загружается печатает фактическую информацию. Схема возвращается в исходное положение,

02-21 12:02:56.900 809-809/com.mindfunk.rething D/layoutInfo: [45,418,1016,1098] 
02-21 12:02:56.950 809-809/com.mindfunk.rething D/layoutInfo: [45,419,1016,1099] 
02-21 12:02:56.984 809-809/com.mindfunk.rething D/layoutInfo: [45,419,1016,1099] 
02-21 12:02:57.084 809-809/com.mindfunk.rething D/layoutInfo: [45,421,1016,1101] 
02-21 12:02:57.569 809-809/com.mindfunk.rething D/layoutInfo: [45,422,1016,1102] 
02-21 12:02:57.936 809-809/com.mindfunk.rething D/layoutInfo: [45,423,1016,1103] 
02-21 12:02:58.077 809-809/com.mindfunk.rething D/ServerRequest: http://RETHING/PHP/getThing.php 
02-21 12:02:58.077 809-809/com.mindfunk.rething D/ServerRequest: Params: IDThing=2 
02-21 12:02:58.873 809-809/com.mindfunk.rething V/AsyncHttpRH: Progress 397 from 397 (100%) 
02-21 12:02:58.888 809-809/com.mindfunk.rething D/ServerRequest: Success: {"res":"1","msg":"ThingProfile2 decargado correctamente","data":{"IDPet":null,"IDUsuario":"29","AliasThing":"ok3","PrecioThing":null,"Localizacion":"Barcelona","Descripcion":"nose","Foto1":"7f1de29e6da19d22b51c68001e7e0e54","Foto2":"a8baa56554f96369ab93e4f3bb068c22","Foto3":"903ce9225fca3e988c2af215d4e544d3","Foto4":"0a09c8844ba8f0936c20bd791130d6b6","Foto5":"2b24d495052a8ce66358eb576b8912c8"}} 
02-21 12:02:58.894 809-809/com.mindfunk.rething D/extendedProfileRequest: Succeed ThingProfile2 decargado correctamente Thing = [email protected] 
02-21 12:02:58.895 809-865/com.mindfunk.rething D/url: http://RETHING/IMAGES/7f1de29e6da19d22b51c68001e7e0e54.jpg 
02-21 12:02:59.632 809-809/com.mindfunk.rething D/loaded conflictiveImage: loaded 
02-21 12:02:59.734 809-809/com.mindfunk.rething D/layoutInfo: [20,20,991,700] 

ответ

0

Хорошо, моя ошибка была с помощью setLeft(), SetTop(), setBottom(), и setRight() методы вместо setTranslationX() и setTranslationY().

Я знаю, что мы должны читать всю документацию, и в нем указаны первые четыре метода, которые могут быть вызваны системой компоновки в любое время, но почему h .. эти методы являются общедоступными, тогда

Смежные вопросы