在可滚动画布上设置按钮

时间:2014-01-04 04:18:16

标签: android android-canvas android-button android-scrollview

我的Android应用程序中有2个ListView。当我从那里选择2个元素时,将出现一个新的窗口(Canvas),并且该画布是可滚动的。一切都好。但是,我想在画布上设置一个按钮。

需要帮助。谢谢。 :)

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:paddingLeft="16dp"
   android:paddingRight="16dp" >

   <ListView
       android:id="@+id/list1"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true" />

   <ListView
       android:id="@+id/list2"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true" />

   <View
        android:id="@+id/mapview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>  

</RelativeLayout>

活性

package com.example.tester1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ScrollView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    Map map;
    //ScrollView sc;
    String Source = "Blank";
    String Destination = "Blank";
    ListView list1;
    ListView list2;
    String [] listArray;
    BufferedReader muri;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            muri = new BufferedReader(
                    new InputStreamReader(getAssets().open("muri.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        listArray = new String[1168];

        String line1;
        int c = 0;// location detector
        try {
            while((line1 = AirportLocation.readLine())!=null){
                StringTokenizer s = new StringTokenizer(line1);
                String airport = s.nextToken();
                listArray[c] = airport;
                c++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        list1 = (ListView) findViewById(R.id.list1);
        list2 = (ListView) findViewById(R.id.list2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        list1.setAdapter(adapter1);
        list2.setAdapter(adapter2);
        list1.setOnItemClickListener(new SourceSelection());
        list2.setOnItemClickListener(new DestinationSelection());

    }

    public void Draw(){
        map = new Map(this);
        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(map);
        setContentView(scrollView); 
        //setContentView(map);
    } 

class SourceSelection implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView temp = (TextView) arg1;
            Source = temp.getText()+"";
            if(!Source.equals("Blank") && !Destination.equals("Blank")){
                Draw();
            }
        }
    }

    class DestinationSelection implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView temp = (TextView) arg1;
            Destination = temp.getText()+"";
            if(!Source.equals("Blank") && !Destination.equals("Blank")){
                Draw();
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public class Map extends View {
        Paint paint = new Paint();
        public Map(Context context) {
            super(context);
        }
        @Override
        public void onDraw(Canvas canvas) {
            paint.setColor(Color.RED);
            canvas.drawCircle(100, 100, 20, paint);
            canvas.drawLine(2000, 2000, 5000, 5000, paint);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // Compute the height required to render the view
            // Assume Width will always be MATCH_PARENT.
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
            setMeasuredDimension(width, height);
        }
    }
}

0 个答案:

没有答案