2016-12-16 122 views
0

我正在Android中绘制线性布局以显示2视图并使用ScaleAnimation进行从右向左缩放。这很好,但webview不是缩放显示如何找到确切的解决方案。如何在左侧缩放或滑动右侧,左侧视图,没有任何视图

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener { 
WebView webview; 
    private ImageView iv; 
    private Matrix matrix = new Matrix(); 
    private float scale = 1f; 
    LinearLayout first,secand,mainLayout; 
    private GestureDetector SGD; 
    float initialX, initialY; 
    boolean border1=false,border2=false; 
    ListView mobile_list; 
    ScrollView scroll; 
    float downXValue,downYValue; 

    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     webview = (WebView) findViewById(R.id.webview); 
     mobile_list=(ListView)findViewById(R.id.mobile_list); 
     ArrayAdapter adapter = new ArrayAdapter<String>(this, 
       R.layout.activity_listview, mobileArray); 
     scroll=(ScrollView)findViewById(R.id.scroll); 
     mobile_list.setAdapter(adapter); 
     webview.loadUrl("http://www.buckscountycouriertimes.com/news/horsham-pfos/pfcs-increasing-in-neshaminy-creek-widespread-in-northampton-wells/article_5ef4bcc8-300e-5f3a-b908-601e79422f49.html"); 
     webview.setPadding(0, 0, 0, 0); 

     webview.getSettings().setLoadWithOverviewMode(true); 
     webview.getSettings().setUseWideViewPort(true); 
     //setupWebView(); 
     first = (LinearLayout) findViewById(R.id.first); 
     secand = (LinearLayout) findViewById(R.id.secand); 
     // SGD=new GestureDetector(this); 
     mainLayout = (LinearLayout) findViewById(R.id.activity_main); 
     /*first.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) { 
      public void onSwipeTop() { 
       Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show(); 
      } 
      public void onSwipeRight() { 
       //Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show(); 
       if(border2) { 
        // int b1 = first.getWidth()/2+8; 
        int width1 = first.getWidth() + 150; 
        int height1 = first.getHeight(); 
        RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(width1, height1); 
        Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rt); 
        //secand.startAnimation(a); 
        secand.animate().x(width1).y(0).setDuration(500).start(); 
        //first.animate().x(width1).y(0).setDuration(500).start(); 
        first.setLayoutParams(parms1); 

        border1=false; 
        border2=false; 
       } 
      } 
      public void onSwipeLeft() { 
       //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show(); 
       if (!border1){ 
        // int b = first.getWidth()/2+8; 
        int width = first.getWidth() - 150; 
        int height = first.getHeight(); 
        RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width, height); 
        Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.lt); 
        secand.animate().x(width).y(0).setDuration(500).start(); 
        // secand.startAnimation(a); 
        first.animate().x(-10).y(0).setDuration(700).start(); 
        first.setLayoutParams(parms); 

        border2=true; 
        border1=true; 
       } 
      } 
      public void onSwipeBottom() { 
       Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
      } 

     }); 
     webview.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) { 
      public void onSwipeTop() { 
       Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show(); 
      } 
      public void onSwipeRight() { 
       //Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show(); 
       if(border2) { 
        // int b1 = first.getWidth()/2+8; 
        int width1 = first.getWidth() + 150; 
        int height1 = first.getHeight(); 
        final  RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(width1, height1); 
        Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rt); 
        //secand.startAnimation(a); 
        //secand.animate().x(width1).y(0).setDuration(500).start(); 

        // first.animate().x(5).y(0).setDuration(700).start(); 
        first.setLayoutParams(parms1); 
        first.requestLayout(); 
        ScaleAnimation anim = new ScaleAnimation(1.0f, 1.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_PARENT,1.0f, Animation.RELATIVE_TO_PARENT, 0.5f); 
        anim.setFillAfter(true); 
        anim.setDuration(900); 
        secand.startAnimation(anim); 


        border1=false; 
        border2=false; 
       } 
      } 
      public void onSwipeLeft() { 
       //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show(); 
       if (!border1){ 
        int b = first.getWidth()/2+8; 
        final int width = first.getWidth() - 150; 
        int height = first.getHeight(); 
        final RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width, height); 
        Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.lt); 

        // secand.startAnimation(a); 
        //first.animate().x(-(80)).y(0).setDuration(500).start(); 
        final Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          //first.setLayoutParams(parms); 
          //first.requestLayout(); 
          // secand.animate().x(width).y(0).setDuration(500).start(); 
          //mainLayout.animate().x(-80).y(0).setDuration(500).start(); 
          first.setLayoutParams(parms); 
          first.requestLayout(); 
          ScaleAnimation anim = new ScaleAnimation(1.0f, 1.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF,1.0f, Animation.RELATIVE_TO_SELF, 0.5f); 
          anim.setFillAfter(true); 
          anim.setDuration(700); 

          secand.startAnimation(anim); 

         } 
        }, 0); 

        border2=true; 
        border1=true; 
       } 
      } 
      public void onSwipeBottom() { 
       Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
      } 

     });*/ 
     secand.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) { 
      public void onSwipeTop() { 
       Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show(); 
      } 
      public void onSwipeRight() { 
       //Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show(); 
       if(border2) { 
        // int b1 = first.getWidth()/2+8; 
        int width1 = first.getWidth() + 150; 
        int height1 = first.getHeight(); 
       final  RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(width1, height1); 
        Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rt); 
        //secand.startAnimation(a); 
        //secand.animate().x(width1).y(0).setDuration(500).start(); 

        // first.animate().x(5).y(0).setDuration(700).start(); 
        // first.setLayoutParams(parms1); 
        // first.requestLayout(); 
        ScaleAnimation anim = new ScaleAnimation(1.5f, 1.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_PARENT,1.0f, Animation.RELATIVE_TO_PARENT, 0.5f); 
        anim.setFillAfter(true); 
        anim.setDuration(300); 
        mainLayout.startAnimation(anim); 


        border1=false; 
        border2=false; 
       } 
      } 
      public void onSwipeLeft() { 
       //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show(); 
       if (!border1){ 
        int b = first.getWidth()/2+8; 
       final int width = first.getWidth() - 150; 
        int height = first.getHeight(); 
        final RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width, height); 
        Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.lt); 

        // secand.startAnimation(a); 
        //first.animate().x(-(80)).y(0).setDuration(500).start(); 
        final Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          //first.setLayoutParams(parms); 
          //first.requestLayout(); 
          // secand.animate().x(width).y(0).setDuration(500).start(); 
          //mainLayout.animate().x(-80).y(0).setDuration(500).start(); 
          //first.setLayoutParams(parms); 
          //first.requestLayout(); 
          //ScaleAnimation anim = new ScaleAnimation(0.8f, 1.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF,1.0f, Animation.RELATIVE_TO_SELF, 0.0f); 
          ScaleAnimation anim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF,1.0f, Animation.RELATIVE_TO_SELF, 0.5f); 
          anim.setFillAfter(true); 
          anim.setDuration(300); 
          mainLayout.startAnimation(anim); 



          //anim.setDuration(0); 
          // webview.startAnimation(anim); 


         } 
        }, 0); 

        border2=true; 
        border1=true; 
       } 
      } 
      public void onSwipeBottom() { 
       Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
      } 

     }); 
     /*webview.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event){ 
       return SGD.onTouchEvent(event); 
      } 
     });*/ 
    /* first.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event){ 
       return SGD.onTouchEvent(event); 
      } 
     }); 
     secand.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event){ 
       return SGD.onTouchEvent(event); 
      } 
     });*/ 
    /* mainLayout.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event){ 
       return SGD.onTouchEvent(event); 
      } 
     });*/ 
     // final GestureDetector gestureDetector = new GestureDetector(this, new GestureListener()); 
     // webview.setGestureDetector(new GestureDetector(new CustomeGestureDetector())); 
    /* webview.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       return GestureListener. 
       int b = first.getWidth()/2+8; 
       int width=first.getWidth()-20; 
       int height=first.getHeight(); 
       RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(b,height); 
       first.setLayoutParams(parms); 
       int width1=first.getWidth()+20; 
       int height1=first.getHeight(); 
       RelativeLayout.LayoutParams parms1= new RelativeLayout.LayoutParams(width1,height1); 
       // secand.setLayoutParams(parms1); 
       try { 
        // Thread.sleep(5000); 
       }catch (Exception e){ 
        e.getMessage(); 
       } 
       //RelativeLayout.LayoutParams parms11 = new RelativeLayout.LayoutParams(width+20,height); 
       //first.setLayoutParams(parms11); 

       return false; 
      } 
     }); 

    }*/ 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     switch (e.getAction()) { 
      case MotionEvent.ACTION_DOWN: { 
       // store the X value when the user's finger was pressed down 
       downXValue = e.getX(); 
       downYValue = e.getY(); 
       break; 
      } 
      case MotionEvent.ACTION_CANCEL:{ 
       float currentX = e.getX(); 
       float currentY = e.getY(); 
       if (Math.abs(downXValue - currentX) > Math.abs(downYValue 
         - currentY)) { 
        Log.v("", "x"); 
        // going backwards: pushing stuff to the right 
        if (downXValue < currentX) { 
         Log.v("", "right"); 

        } 

        // going forwards: pushing stuff to the left 
        if (downXValue > currentX) { 
         Log.v("", "left"); 

        } 
       } 
       break; 
      } 
     } 
     return false; 
    } 


    @Override 
    public void onShowPress(MotionEvent e) { 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     return false; 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 

    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     String LOGTAG="CAT"; 
     switch (getSlope(e1.getX(), e1.getY(), e2.getX(), e2.getY())) { 
      case 1: 
       Log.d(LOGTAG, "top"); 
       return true; 
      case 2: 
       Log.d(LOGTAG, "left"); 
       if (!border1){ 
        // int b = first.getWidth()/2+8; 
        int width = first.getWidth() - 80; 
       int height = first.getHeight(); 
       RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width, height); 
        Animation a = AnimationUtils.loadAnimation(this, R.anim.lt); 
        secand.animate().x(width).y(0).setDuration(500).start(); 
        // secand.startAnimation(a); 
        first.animate().x(-10).y(0).setDuration(700).start(); 
        first.setLayoutParams(parms); 

        border2=true; 
        border1=true; 
     } 
       return false; 
      case 3: 
       Log.d(LOGTAG, "down"); 
       return false; 
      case 4: 
       Log.d(LOGTAG, "right"); 
       Log.d(LOGTAG, "left"); 
       if(border2) { 
        // int b1 = first.getWidth()/2+8; 
        int width1 = first.getWidth() + 80; 
        int height1 = first.getHeight(); 
        RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(width1, height1); 
        Animation a = AnimationUtils.loadAnimation(this, R.anim.rt); 
        //secand.startAnimation(a); 
        secand.animate().x(width1).y(0).setDuration(500).start(); 
        //first.animate().x(width1).y(0).setDuration(500).start(); 
        first.setLayoutParams(parms1); 

        border1=false; 
        border2=false; 
       } 
       return false; 
     } 
     return false; 
    } 
    /* @Override 
    public boolean onTouchEvent(MotionEvent event) 
    { 
     //method onTouchEvent of GestureDetector class Analyzes the given motion event 
     //and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied. 
     //Returns true if the GestureDetector.OnGestureListener consumed the event, else false. 

     boolean eventConsumed=SGD.onTouchEvent(event); 
     if (eventConsumed) 
     { 
      // Toast.makeText(this,GestureListener.currentGestureDetected,Toast.LENGTH_SHORT).show(); 
      Log.e("Action",GestureListener.currentGestureDetected); 
      return true; 
     } 
     else 
      return false; 

    }*/ 
    private int getSlope(float x1, float y1, float x2, float y2) { 
     Double angle = Math.toDegrees(Math.atan2(y1 - y2, x2 - x1)); 
     if (angle > 45 && angle <= 135) 
      // top 
      return 1; 
     if (angle >= 135 && angle < 180 || angle < -135 && angle > -180) 
      // left 
      return 2; 
     if (angle < -45 && angle>= -135) 
      // down 
      return 3; 
     if (angle > -45 && angle <= 45) 
      // right 
      return 4; 
     return 0; 
    } 
    public static void expand(final View v) { 
     v.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     final int targetHeight = v.getMeasuredHeight(); 

     // Older versions of android (pre API 21) cancel animations for views with a height of 0. 
     v.getLayoutParams().height = 1; 
     v.setVisibility(View.VISIBLE); 
     Animation a = new Animation() 
     { 
      @Override 
      protected void applyTransformation(float interpolatedTime, Transformation t) { 
       v.getLayoutParams().height = interpolatedTime == 1 
         ? RelativeLayout.LayoutParams.WRAP_CONTENT 
         : (int)(targetHeight * interpolatedTime); 
       v.requestLayout(); 
      } 

      @Override 
      public boolean willChangeBounds() { 
       return true; 
      } 
     }; 

     // 1dp/ms 
     a.setDuration((int)(targetHeight/v.getContext().getResources().getDisplayMetrics().density)); 
     v.startAnimation(a); 
    } 

    public static void collapse(final View v) { 
     final int initialHeight = v.getMeasuredHeight(); 

     Animation a = new Animation() 
     { 
      @Override 
      protected void applyTransformation(float interpolatedTime, Transformation t) { 
       if(interpolatedTime == 1){ 
        v.setVisibility(View.GONE); 
       }else{ 
        v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime); 
        v.requestLayout(); 
       } 
      } 

      @Override 
      public boolean willChangeBounds() { 
       return true; 
      } 
     }; 

     // 1dp/ms 
     a.setDuration((int)(initialHeight/v.getContext().getResources().getDisplayMetrics().density)); 
     v.startAnimation(a); 
    } 
    private void setupWebView() { 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onPageFinished(WebView view, String url) { 
       webview.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)"); 
       super.onPageFinished(view, url); 
      } 
     }); 
     webview.addJavascriptInterface(this, "MyApp"); 
    } 
    @JavascriptInterface 
    public void resize(final float height) { 
     MainActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       webview.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density))); 
      } 
     }); 
    } 
    private int getScale(){ 
     Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     int width = display.getWidth(); 
     Double val = new Double(width)/new Double(5); 
     val = val * 100d; 
     return val.intValue(); 
    } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 

    tools:context="com.calkins.slide_right_to_left.MainActivity"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:layout_height="150dp"> 
</LinearLayout> 
    <LinearLayout 
     android:id="@+id/mainLayout" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="match_parent"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="300dp" 
     android:orientation="horizontal" 
     android:id="@+id/first" 
     android:layout_height="match_parent"> 
     <ListView 
      android:id="@+id/mobile_list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </ListView> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:id="@+id/secand" 
     android:layout_toRightOf="@+id/first" 
     android:layout_height="match_parent"> 
     <ImageView 
      android:layout_width="match_parent" 
      android:background="@drawable/new_phone" 
      android:layout_height="250dp" /> 
     <ScrollView 
      android:id="@+id/scroll" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:scrollbars="vertical" > 
     <WebView 
      android:id="@+id/webview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 
     </ScrollView> 
    </LinearLayout> 
</RelativeLayout> 

    </LinearLayout> 

</LinearLayout> 

我想扫就像下图:

它是真实的图像enter image description here

而且我想只是想:enter image description here

回答

0

使用这一个

public void onSwipeRight() { 
       //Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show(); 
       if (border2) { 

        mainLayout.animate().x(5).y(0).setDuration(500).start(); 


        final Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          int x1 = first.getWidth()/2; 
          // int y=first.getHeight(); 
          int rightShift = secand.getWidth(); 
          ViewGroup.LayoutParams params=mainLayout.getLayoutParams(); 
          params.width=rightShift; 
          mainLayout.setLayoutParams(params); 

         } 
        }, 450); 
        border1 = false; 
        border2 = false; 
       } 
      } 



public void onSwipeLeft() { 
       //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show(); 
       if (!border1) { 
        int x1 = first.getWidth()/2; 
        int y=first.getHeight(); 
        int rightShift = secand.getWidth() + x1; 
        mainLayout.animate().x(-x1).y(0).setDuration(500).start(); 
       final ViewGroup.LayoutParams params=mainLayout.getLayoutParams(); 
        params.width=2*rightShift; 

        final Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          mainLayout.setLayoutParams(params); 
         } 
        },1/2); 

        border2 = true; 
        border1 = true; 
       } 
      }