Java tutorial
package com.pixby.texo.EditTools; /* * Copyright (c) 2016 David Allen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.util.ArrayMap; import android.view.View; import android.widget.ImageButton; import android.widget.RadioButton; import com.pixby.texo.R; import java.util.Map; public class PositionTool extends ToolFragment { private static final int[] ButtonIdArray = { R.id.buttonN, R.id.buttonNE, R.id.buttonE, R.id.buttonSE, R.id.buttonS, R.id.buttonSW, R.id.buttonW, R.id.buttonNW, R.id.buttonCENTER }; private final ArrayMap<Integer, Position> map = new ArrayMap<>(); private View.OnClickListener mButtonClickHandler; private RadioButton mCurrentChecked; public static PositionTool newInstance() { PositionTool f = new PositionTool(); f.mToolViewId = R.layout.position_tool; return f; } private Position idToPosition(int id) { return map.get(id); } private int positionToId() { for (Map.Entry<Integer, Position> entry : map.entrySet()) { if (entry.getValue() == Position.CENTER) { return entry.getKey(); } } return R.id.buttonCENTER; } private void initMap() { map.put(R.id.buttonN, Position.N); map.put(R.id.buttonNE, Position.NE); map.put(R.id.buttonE, Position.E); map.put(R.id.buttonSE, Position.SE); map.put(R.id.buttonS, Position.S); map.put(R.id.buttonSW, Position.SW); map.put(R.id.buttonW, Position.W); map.put(R.id.buttonNW, Position.NW); map.put(R.id.buttonCENTER, Position.CENTER); } private void initButtons(View v) { for (int id : ButtonIdArray) { RadioButton b = (RadioButton) v.findViewById(id); if (b != null) { b.setOnClickListener(mButtonClickHandler); } } } @Override public void onStart() { super.onStart(); init(); } /* set button to center by default framework will set on orientation change */ private void init() { mCurrentChecked = getCheckedButton(); if (mCurrentChecked == null) { mCurrentChecked = (RadioButton) getView().findViewById(positionToId()); if (mCurrentChecked != null) { mCurrentChecked.setChecked(true); } } } @SuppressWarnings("ConstantConditions") private RadioButton getCheckedButton() { View parentView = getView(); for (int id : ButtonIdArray) { RadioButton b = (RadioButton) parentView.findViewById(id); if (b != null && b.isChecked()) { return b; } } return null; } @Override public void onViewCreated(View v, @Nullable Bundle savedInstanceState) { super.onViewCreated(v, savedInstanceState); initMap(); mButtonClickHandler = new View.OnClickListener() { @Override public void onClick(View v) { // uncheck prior button mCurrentChecked.setChecked(false); mCurrentChecked = (RadioButton) v; Position pos = idToPosition(mCurrentChecked.getId()); getWatermark().moveToLogicalPosition(pos, getEditView().getImageIntrinsicBounds()); getEditView().invalidate(); } }; initButtons(v); ImageButton okButton = (ImageButton) v.findViewById(R.id.save_button); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getListener().onSave(getTag()); } }); ImageButton cancelButton = (ImageButton) v.findViewById(R.id.cancel_button); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getEditView().invalidate(); getListener().onCancel(getTag()); } }); } }