Arcgis for Android—图层控制
生活就是要不停的总结…..
这是一篇关于图层控制的博客,希望对你的学习带来帮助,好了,废话不多说,直接上代码。
1.MainActivity.java代码:
public class MainActivity extends AppCompatActivity {
MapView map;
ArrayList filenameslist;
ArrayList titlelayerslist;
ArrayList Envelopelist;
ArcGISLocalTiledLayer localtilayer1;
GraphicsLayer drawLayer;
private Button DialogButton2;
private static HashMap isCheckedLayer;
private static boolean[] IsCheckedArry;//= new boolean[]{false,false,false,false};
private static String[] ItemsArry;//= new String[]{false,false,false,false};
private SeekBar seekbar;
private TextView seekbarTextView;
private static final String TAG="ShowErr";
private static int Opacity=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (MapView) findViewById(R.id.map);
localtilayer1 = new ArcGISLocalTiledLayer("file:///storage/emulated/0/FpWorks/Map/可乐乡.tpk");
map.addLayer(localtilayer1);
map.setExtent(localtilayer1.getExtent());
titlelayerslist = new ArrayList();
Envelopelist = new ArrayList();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File path = new File(Environment.getExternalStorageDirectory() + "/FpWorks/Layers/");// 获得SD卡路径
// File path = new File("/mnt/sdcard/");
File[] files = path.listFiles();// 读取
filenameslist = getFileName(files, ".tpk");
}
IsCheckedArry=new boolean[filenameslist.size()];
ItemsArry=new String[filenameslist.size()];
for (int i = 0; i < filenameslist.size(); i++) {
IsCheckedArry[i]=false;
ItemsArry[i]= filenameslist.get(i).toString();
String path = Environment.getExternalStorageDirectory() + "/FpWorks/Layers/" + filenameslist.get(i).toString();
ArcGISLocalTiledLayer localtilayer = new ArcGISLocalTiledLayer(path);
localtilayer.setVisible(false);
titlelayerslist.add(localtilayer);
Envelopelist.add(localtilayer.getExtent());
map.addLayer(localtilayer);
}
drawLayer = new GraphicsLayer();
map.addLayer(drawLayer);
DialogButton2=(Button)findViewById(R.id.LayerControlBtn);
DialogButton2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View textEntryView = factory.inflate(R.layout.seekbar_control,null);
seekbarTextView=(TextView)textEntryView.findViewById(R.id.SeekBarTitle);
seekbar=(SeekBar)textEntryView.findViewById(R.id.TransparencySeekBar);
seekbar.setProgress(Opacity);
seekbar.setOnSeekBarChangeListener(seekBarListener);
new AlertDialog.Builder(MainActivity.this)
.setTitle("图层控制")
.setIcon(android.R.drawable.ic_menu_view)
.setView(textEntryView)
.setMultiChoiceItems(ItemsArry, IsCheckedArry, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
Log.d("ShowItems", "序号=" + which + "IsChected='" + isChecked + "'");
titlelayerslist.get(which).setVisible(isChecked);
}
})
.show();
}
});
}
private SeekBar.OnSeekBarChangeListener seekBarListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.i(TAG, "onStopTrackingTouch");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i(TAG, "onStartTrackingTouch");
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.i(TAG, "onProgressChanged");
seekbarTextView.setText("当前透明度为: -" + progress);
// float Opacity=0.5f;
Opacity=progress;
for (ArcGISLocalTiledLayer layer:titlelayerslist) {
layer.setOpacity((float)progress/100);
}
}
};
/**
* 获取内置SD卡路径
*
* @return
*/
public String getInnerSDCardPath() {
return Environment.getExternalStorageDirectory().getPath();
}
public void SelectClick(View v) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File path = new File(Environment.getExternalStorageDirectory() + "/FpWorks/Layers/");// 获得SD卡路径
// File path = new File("/mnt/sdcard/");
File[] files = path.listFiles();// 读取
getFileName(files, ".xls");
ArrayList filenameslist = getFileName(files, ".tpk");
AddItemsToProjectDaig(filenameslist);
}
}
private static int Choiceflag = 0;
/**
* //点击方案弹出的对话框
*
* @param namesList
*/
private void AddItemsToProjectDaig(final ArrayList namesList) {
String items0[] = new String[namesList.size()];
for (int i = 0, j = namesList.size(); i < j; i++) {
items0[i] = namesList.get(i);
}
final String items[] = items0;
new AlertDialog.Builder(MainActivity.this)
.setTitle("请点击选择")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Choiceflag = which;
new AlertDialog.Builder(
MainActivity.this)
.setTitle("你选择了:" + items[which])
.setMessage("点击选择操作")
.setPositiveButton(
"确定",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
String name1 = items[Choiceflag].toString();
Log.d("Test", items[Choiceflag].toString());
Log.d("Test", filenameslist.get(0).toString());
for (int i = 0; i < filenameslist.size(); i++) {
String name2 = items[i].toString();
Log.d("Test", name2);
// Log.d("Test",filenameslist.get(i).toString());
if (name1.equals(name2)) {
Log.d("Test", "name1=" + name2);
Log.d("Test", "name2=" + name1);
Log.d("Test", "你选择的是" + filenameslist.get(i).toString());
titlelayerslist.get(i).setVisible(true);
map.setExtent(Envelopelist.get(i));
} else {
titlelayerslist.get(i).setVisible(false);
}
}
}
})
.setNegativeButton(
"取消",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
// 这里点击取消之后可以进行的操作
}
}).show();
}
}).show();
}
private ArrayList getFileName(File[] files, String Filter) {
ArrayList filenameslist = new ArrayList();
if (files != null) {// 先判断目录是否为空,否则会报空指针
for (File file : files) {
if (file.isDirectory()) {
Log.i("zeng", "若是文件目录。继续读1" + file.getName().toString()
+ file.getPath().toString());
getFileName(file.listFiles(), Filter);
Log.i("zeng", "若是文件目录。继续读2" + file.getName().toString()
+ file.getPath().toString());
} else {
String fileName = file.getName();
if (fileName.endsWith(Filter)) {//".db"
filenameslist.add(fileName);
}
}
}
}
return filenameslist;
}
}
2.Main.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="smgi.com.fpmap.MainActivity">
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.esri.android.map.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
com.esri.android.map.MapView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tst"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="SelectClick" />
<Button
android:id="@+id/LayerControlBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图层控制"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
/>
LinearLayout>
FrameLayout>
LinearLayout>
3.seekbar_control.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/SeekBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dip"
android:text="透明度"
android:textSize="16sp"
android:textStyle="bold" />
<SeekBar
android:id="@+id/TransparencySeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
LinearLayout>