使用onFling进行平滑滚动?

时间:2017-03-19 05:51:00

标签: android android-layout

我反编译了一个显示PDF的应用程序,但我的问题是我想实现一个连续的垂直滚动(就像谷歌PDF阅读器一样)......

任何帮助都会得到极大的赞赏......

以下是其PageView的代码 -

public class PageView extends View implements OnGestureListener, OnScaleGestureListener {
   protected DocumentActivity actionListener;
   protected Bitmap bitmap;
   protected int bitmapH;
   protected int bitmapW;
   protected int canvasH;
   protected int canvasW;
   protected GestureDetector detector;
   protected boolean error;
   protected Paint errorPaint;
   protected Path errorPath;
   protected Paint linkPaint;
   protected Link[] links;
   protected float maxScale;
   protected float minScale;
   protected ScaleGestureDetector scaleDetector;
   protected int scrollX;
   protected int scrollY;
   protected Scroller scroller;
   protected boolean showLinks;
   protected float viewScale;

   public PageView(Context var1, AttributeSet var2) {
      super(var1, var2);
      this.scroller = new Scroller(var1);
      this.detector = new GestureDetector(var1, this);
      this.scaleDetector = new ScaleGestureDetector(var1, this);
      this.viewScale = 1.0F;
      this.minScale = 1.0F;
      this.maxScale = 2.0F;
      this.linkPaint = new Paint();
      this.linkPaint.setARGB(32, 0, 0, 255);
      this.errorPaint = new Paint();
      this.errorPaint.setARGB(255, 255, 80, 80);
      this.errorPaint.setStrokeWidth(5.0F);
      this.errorPaint.setStyle(Style.STROKE);
      this.errorPath = new Path();
      this.errorPath.moveTo(-100.0F, -100.0F);
      this.errorPath.lineTo(100.0F, 100.0F);
      this.errorPath.moveTo(100.0F, -100.0F);
      this.errorPath.lineTo(-100.0F, 100.0F);
   }

   public void goBackward() {
      this.scroller.forceFinished(true);
      if(this.scrollY <= 0) {
         if(this.scrollX <= 0) {
            this.actionListener.goBackward();
            return;
         }

         this.scroller.startScroll(this.scrollX, this.scrollY, 9 * -this.canvasW / 10, this.bitmapH - this.canvasH - this.scrollY, 500);
      } else {
         this.scroller.startScroll(this.scrollX, this.scrollY, 0, 9 * -this.canvasH / 10, 250);
      }

      this.invalidate();
   }

   public void goForward() {
      this.scroller.forceFinished(true);
      if(this.scrollY + this.canvasH >= this.bitmapH) {
         if(this.scrollX + this.canvasW >= this.bitmapW) {
            this.actionListener.goForward();
            return;
         }

         this.scroller.startScroll(this.scrollX, this.scrollY, 9 * this.canvasW / 10, -this.scrollY, 500);
      } else {
         this.scroller.startScroll(this.scrollX, this.scrollY, 0, 9 * this.canvasH / 10, 250);
      }

      this.invalidate();
   }

   public boolean onDown(MotionEvent var1) {
      this.scroller.forceFinished(true);
      return true;
   }

   public void onDraw(Canvas var1) {
      if(this.bitmap == null) {
         if(this.error) {
            var1.translate((float)(this.canvasW / 2), (float)(this.canvasH / 2));
            var1.drawPath(this.errorPath, this.errorPaint);
         }
      } else {
         if(this.scroller.computeScrollOffset()) {
            this.scrollX = this.scroller.getCurrX();
            this.scrollY = this.scroller.getCurrY();
            this.invalidate();
         }

         int var2;
         if(this.bitmapW <= this.canvasW) {
            this.scrollX = 0;
            var2 = (this.canvasW - this.bitmapW) / 2;
         } else {
            if(this.scrollX < 0) {
               this.scrollX = 0;
            }

            if(this.scrollX > this.bitmapW - this.canvasW) {
               this.scrollX = this.bitmapW - this.canvasW;
            }

            var2 = -this.scrollX;
         }

         int var3;
         if(this.bitmapH <= this.canvasH) {
            this.scrollY = 0;
            var3 = (this.canvasH - this.bitmapH) / 2;
         } else {
            if(this.scrollY < 0) {
               this.scrollY = 0;
            }

            if(this.scrollY > this.bitmapH - this.canvasH) {
               this.scrollY = this.bitmapH - this.canvasH;
            }

            var3 = -this.scrollY;
         }

         var1.translate((float)var2, (float)var3);
         var1.scale(this.viewScale, this.viewScale);
         var1.drawBitmap(this.bitmap, 0.0F, 0.0F, (Paint)null);
         if(this.showLinks && this.links != null) {
            Link[] var4 = this.links;
            int var5 = var4.length;

            for(int var6 = 0; var6 < var5; ++var6) {
               Rect var7 = var4[var6].bounds;
               var1.drawRect(var7.x0, var7.y0, var7.x1, var7.y1, this.linkPaint);
            }
         }
      }

   }

   public boolean onFling(MotionEvent var1, MotionEvent var2, float var3, float var4) {
      if(this.bitmap != null) {
         int var5;
         if(this.bitmapW > this.canvasW) {
            var5 = this.bitmapW - this.canvasW;
         } else {
            var5 = 0;
         }

         int var6;
         if(this.bitmapH > this.canvasH) {
            var6 = this.bitmapH - this.canvasH;
         } else {
            var6 = 0;
         }

         this.scroller.forceFinished(true);
         this.scroller.fling(this.scrollX, this.scrollY, (int)(-var3), (int)(-var4), 0, var5, 0, var6);
         this.invalidate();
      }

      return true;
   }

   public void onLongPress(MotionEvent var1) {
      boolean var2;
      if(!this.showLinks) {
         var2 = true;
      } else {
         var2 = false;
      }

      this.showLinks = var2;
      this.invalidate();
   }

   public boolean onScale(ScaleGestureDetector var1) {
      if(this.bitmap != null) {
         float var2 = var1.getFocusX();
         float var3 = var1.getFocusY();
         float var4 = var1.getScaleFactor();
         float var5 = (var2 + (float)this.scrollX) / this.viewScale;
         float var6 = (var3 + (float)this.scrollY) / this.viewScale;
         this.viewScale = var4 * this.viewScale;
         if(this.viewScale < this.minScale) {
            this.viewScale = this.minScale;
         }

         if(this.viewScale > this.maxScale) {
            this.viewScale = this.maxScale;
         }

         this.bitmapW = (int)((float)this.bitmap.getWidth() * this.viewScale);
         this.bitmapH = (int)((float)this.bitmap.getHeight() * this.viewScale);
         this.scrollX = (int)(var5 * this.viewScale - var2);
         this.scrollY = (int)(var6 * this.viewScale - var3);
         this.scroller.forceFinished(true);
         this.invalidate();
      }

      return true;
   }

   public boolean onScaleBegin(ScaleGestureDetector var1) {
      return true;
   }

   public void onScaleEnd(ScaleGestureDetector var1) {
   }

   public boolean onScroll(MotionEvent var1, MotionEvent var2, float var3, float var4) {
      if(this.bitmap != null) {
         this.scrollX += (int)var3;
         this.scrollY += (int)var4;
         this.scroller.forceFinished(true);
         this.invalidate();
      }

      return true;
   }

   public void onShowPress(MotionEvent var1) {
   }

   public boolean onSingleTapUp(MotionEvent var1) {
      float var2 = var1.getX();
      float var3 = var1.getY();
      boolean var4 = this.showLinks;
      boolean var5 = false;
      if(var4) {
         Link[] var8 = this.links;
         var5 = false;
         if(var8 != null) {
            float var9;
            if(this.bitmapW <= this.canvasW) {
               var9 = (float)((this.bitmapW - this.canvasW) / 2);
            } else {
               var9 = (float)this.scrollX;
            }

            float var10;
            if(this.bitmapH <= this.canvasH) {
               var10 = (float)((this.bitmapH - this.canvasH) / 2);
            } else {
               var10 = (float)this.scrollY;
            }

            float var11 = (var2 + var9) / this.viewScale;
            float var12 = (var3 + var10) / this.viewScale;
            Link[] var13 = this.links;
            int var14 = var13.length;
            int var15 = 0;

            while(true) {
               var5 = false;
               if(var15 >= var14) {
                  break;
               }

               Link var16 = var13[var15];
               Rect var17 = var16.bounds;
               if(var11 >= var17.x0 && var11 <= var17.x1 && var12 >= var17.y0 && var12 <= var17.y1) {
                  if(var16.page >= 0) {
                     this.actionListener.gotoPage(var16.page);
                  } else if(var16.uri != null) {
                     this.actionListener.gotoURI(var16.uri);
                  }

                  var5 = true;
                  break;
               }

               ++var15;
            }
         }
      }

      if(!var5) {
         float var6 = (float)(this.canvasW / 3);
         float var7 = var6 * 2.0F;
         if(var2 <= var6) {
            this.goBackward();
         }

         if(var2 >= var7) {
            this.goForward();
         }

         if(var2 > var6 && var2 < var7) {
            this.actionListener.toggleUI();
         }
      }

      this.invalidate();
      return true;
   }

   public void onSizeChanged(int var1, int var2, int var3, int var4) {
      this.canvasW = var1;
      this.canvasH = var2;
      this.actionListener.onPageViewSizeChanged(var1, var2);
   }

   public boolean onTouchEvent(MotionEvent var1) {
      this.detector.onTouchEvent(var1);
      this.scaleDetector.onTouchEvent(var1);
      return true;
   }

   public void setActionListener(DocumentActivity var1) {
      this.actionListener = var1;
   }

   public void setBitmap(Bitmap var1, boolean var2, Link[] var3) {
      if(this.bitmap != null) {
         this.bitmap.recycle();
      }

      this.error = false;
      this.links = var3;
      this.bitmap = var1;
      this.bitmapW = (int)((float)this.bitmap.getWidth() * this.viewScale);
      this.bitmapH = (int)((float)this.bitmap.getHeight() * this.viewScale);
      this.scroller.forceFinished(true);
      int var4;
      if(var2) {
         var4 = this.bitmapW - this.canvasW;
      } else {
         var4 = 0;
      }

      this.scrollX = var4;
      int var5 = 0;
      if(var2) {
         var5 = this.bitmapH - this.canvasH;
      }

      this.scrollY = var5;
      this.invalidate();
   }

   public void setError() {
      if(this.bitmap != null) {
         this.bitmap.recycle();
      }

      this.error = true;
      this.links = null;
      this.bitmap = null;
      this.invalidate();
   }
}

0 个答案:

没有答案