I'm rewriting my Kivy
app to Java
and XML
files.
What is an equivalent to ScatterLayout
?
I have this code:
class MyScatter(ScatterLayout):
pass
Builder.load_string('''
<MyScatter>:
scale: 1
do_scale: True
do_translation: True
do_rotation: False
auto_bring_to_front: False
Image:
id: img
source: ''
size_hint: (0.43, 0.6)
pos_hint: {'center_x': 0.33, 'center_y': 0.5}
keep_ratio: True
''')
...
def build(self):
mainbox = FloatLayout()
self.ms = MyScatter()
mainbox.add_widget(self.ms)
...
What I want to achieve is the scaling functionality of an image.
For scaling an image in Android, you can use an ImageView
along with a ScaleGestureDetector for handling scaling based on multi-touch events.
Here's a basic example of how you could set this up in Java:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private ScaleGestureDetector scaleGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = detector.getScaleFactor();
scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f));
imageView.setScaleX(scaleFactor);
imageView.setScaleY(scaleFactor);
return true;
}
}
}
For the corresponding XML layout file (activity_main.xml), you would have something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image_here" />
</RelativeLayout>