关于canvas画统计图与统计图的水平或坚直滑动

来源:互联网 发布:网络宣传部是干什么的 编辑:程序博客网 时间:2024/04/26 22:02

前段时间出于项目需求公司需要用到制作折线图或者柱状图由于一般的统计图都是很宽的,手机屏幕又太窄,所以需要折线图能够水平滑动,当时想到了HorizontalScrollview 但是当使用horizontalscrollview.addview();这个方法时被告知添加的view里面不能出现ondraw或者draw方法,当时为了解决问题,临时出了view类里面的ontouch事件监听手指的移动来改变view的scrollX或scrollY的方法来实现图表的水平移动。但是今天的客户提出了一个要求需要同一个activity里面出现三张表可以上下滑动,经过翻阅api以及尝试终于解决问题,帖出来作为自己的第一个blog;
package com.example.horizontal_relative;import com.example.chart.Chart1;import com.example.chart.Chart2;import com.example.chart.Chart3;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.widget.RelativeLayout;public class ScrollViewAcitvity extends Activity {private RelativeLayout layout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.scroll_main);layout = (RelativeLayout) findViewById(R.id.layout);View scrollView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.scroll_view, null);RelativeLayout layout1 = (RelativeLayout) scrollView.findViewById(R.id.container1);View horizontalView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.horizontal_scroll_view, null);RelativeLayout layout_h = (RelativeLayout)horizontalView.findViewById(R.id.container);Chart1 chart1 = new Chart1(getApplicationContext());layout_h.addView(chart1);layout1.addView(horizontalView);RelativeLayout layout2 = (RelativeLayout) scrollView.findViewById(R.id.container2);Chart2 chart2 = new Chart2(getApplicationContext());layout2.addView(chart2);RelativeLayout layout3 = (RelativeLayout) scrollView.findViewById(R.id.container3);Chart3 chart3 = new Chart3(getApplicationContext());layout3.addView(chart3);layout.removeAllViews();layout.addView(scrollView);}}

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical"         >        <RelativeLayout            android:id="@+id/container1"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:padding="2.0dip" />        <RelativeLayout            android:id="@+id/container2"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:padding="2.0dip" />        <RelativeLayout            android:id="@+id/container3"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:padding="2.0dip" />    </LinearLayout></ScrollView>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/main_background"     >    <RelativeLayout        android:id="@+id/layout"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true" >    </RelativeLayout></RelativeLayout>

0 0