2014-11-29 4 views
0

제 문제를 처리하기 위해 많은 노력을했지만 실제로 작동하지 않았고 Google도 나를 도울 수 없었습니다. Canvas가 포함 된 ImageView가 있습니다. 캔버스는 그래프의 일부를 그립니다. 따라서 사용자가 (가로로) 스크롤 할 때 캔버스는 그래프의 다른 부분을 그려야합니다. 예를 들어ScrollView없이 이미지 뷰 (캔버스 포함)에서 스크롤 제스처를 분석하고 처리합니다.

캔버스 폭은 캔버스에서 X = 0의 그래프를 그리는 200 픽셀

이고 X = 200

사용자가 스크롤을 (수평), 그는 100 픽셀

통해 자신의 손가락을 움직이는 캔버스 이제 X = 100 내지 그래프를 그린다 X = 300

캔버스는 모든 픽셀에 의해 묘화해야이 솔루션은 작동하지 않았 (부드러운 스크롤) 사용자가 스크롤 : WH 그리기 ole 그래프를 스크롤하여 Scrollview에 넣습니다. 캔버스/비트 맵이 너무 크기 때문에 OutOfMemoryError를 전달합니다.

또한 다른 작업을 수행해야하므로 위에서 설명한 것처럼이 작업을 수행해야합니다.

XML :

<de.touristenfahrerforum.Marcel.Fragments.SpeedGraph 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:id="@+id/graph" 
    android:background="@android:color/black" 
    android:name="de.touristenfahrerforum.MarcelMoiser.Fragments.SpeedGraph"> 
</de.touristenfahrerforum.Marcel.Fragments.SpeedGraph> 

자바 클래스 : 여기

는 코드입니다

public class SpeedGraph extends ImageView 
{ 
... 
public SpeedGraph(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public SpeedGraph(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public SpeedGraph(Context context) { 
    super(context); 
} 

public void setup(ArrayList<Location> locations, ScrollViewListener scrollViewListener) 
{ 
... 
    Bitmap bitmap = Bitmap.createBitmap(speedCanvasWidth,speedCanvasHeight,Bitmap.Config.ARGB_8888); 
    canvas  = new Canvas(bitmap); 
    this.setImageBitmap(bitmap); 
...} 
... 
private void drawGraph(int start, int end, int color) 
{ 
    graphPaint.setColor(color); 

    long startTime = locations.get(start).getTime(); 

    Path path = new Path(); 
    path.moveTo((locations.get(start).getTime()-startTime)/10*V.LOGICAL_DENSITY, speedCanvasHeight-(int)(locations.get(start).getSpeed()*3.6*SIZE_FACTOR)); 

    for(int it = start; it < end; it++) 
    { 
     Location location = locations.get(it); 
     path.lineTo((location.getTime()-startTime)/10*V.LOGICAL_DENSITY, speedCanvasHeight-(int)(location.getSpeed()*3.6*SIZE_FACTOR)); 
    } 

    canvas.drawPath(path,graphPaint); 
} 
... 

그래서 수평 스크롤 제스처를 인식하고 나에게의 수를 제공합니다 뭔가를 구현하고 싶습니다 사용자가 스크롤 한 픽셀을 나타내는 픽셀

사전

답변

0

그것은 아무도 대답하지 않는다는 처음의에서 너희들을 감사,하지만 난 내 문제를 해결하는 방법을 알아 냈어. 가장 좋은 방법은 GestureDetector.OnGestureListener를 구현하고 모든 메서드를 덮어 쓰는 것입니다. 또한 GestureDetector 객체를 만들어야합니다. OnScroll (...) 메서드는 사용자가 스크롤 한 픽셀을 찾는 곳입니다.

public class SpeedGraph extends ImageView implements GestureDetector.OnGestureListener 
{ 
    private Context context; 
... 
public void setup(...) 
{ 
    this.gestureDetector = new GestureDetector(context, this); 
    gestureDetector.setIsLongpressEnabled(false); 
...} 
@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
{ 
    scrolled+=distanceX; 
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
    drawGrid(); 
    redrawGraphsInCanvas(); 
    this.invalidate(); 
    return true; 
} 

@Override 
public boolean dispatchTouchEvent(MotionEvent ev){ 
    gestureDetector.onTouchEvent(ev); 
    return true; 
} 

...

: 여기 몇 가지 코드