last day (16 days later) » 

7:18 PM
1
A: Zoom in/out the whole page layout

LunarWatcherFirst, lets start simple. Scaling is relatively easy. (this code is not used in the further examples): TextView rootView; rootView.setScaleX(sx); rootView.setScaleY(sx); sx and sy is scale[X/Y] That is the fundamentals of scaling. Now we go to the hard part: Pinch zoom. this requi...

 
Thanks a million Lunar. It still sounds very complicated to me. Hope you don't mind if I'll keep asking you for more help on the way regarding this topic:) Of course I'll upvote your answer right now.
 
If there is something that doesn't add up, just ask and I will answer your concerns. Zooming is in general a hard topic because in addition to zoom, there is also touch events for pinch and pan that has to add up. The code I presented in my answer is something I used in a SurfaceView and using a Canvas. if this for some reason doesn't work, I will help you through the process of creating your own textview where you handle zoom inside the class.
@Ramona I have just become aware of a better way when using textViews. Testing it out and will add it to the answer when I get it to work.
 
Ok Lunar. Just also please let me know what I should implement the first. Reading your post I got a bit lost :) Thank you
 
Well, it is really up to you. The second shows how you can create a zoomable textview yo ucan use anywhere. The first shows how you can implement zoom on anything else. The custom textview adds scroll if necessary, and can be implemented as a custom view even in xml. Which you select is up to you, but the second method works differently as it changes the size of the text based on zoom.
 
But is zoomable textView going to zoom the whole page/screen? Or just the text itself , so the text will be zoomed but picture on the page stays the same?
 
7:18 PM
This is like with every other view: If you want it to cover the page, set width and height to match parent. If you want it to cover a specific dimension, you make it cover those dimensions. The zoom doesn't change the physical size, only the text size. It only changes the size of the view if either width or height is set to wrap_content . So if you set the width and height to 100x100, a scrolling will be added vertically to handle the new height. The textview changes size so it remains the same so the page itself doesn't zoom. For that you should change the RelativeLayout, but you should(1/2
(2/2) instead add a ScrollView inside the root view with a RelativeLayout with the page in it. you can keep the root relative layout if you want. Then you use the first method on the RelativeLayout.
 
wow! now i'm totally lost :) you explain it so well and at the same time i don't know what to do :) will get back here in 2h
 
The best thing you can do at this point is try the different solutions presented in my answer, so you can see what works for you while learning how it works.
 
Thanks I will try and surely will be asking you for gudiance :)
And first question - what to put here [ANY-ACCESS-MODIFIER]?
 
[ANY-ACCESS-MODIFIER] is my way of saying you can put private, public or no special access. It is up to you and based on your needs. An access modifier are primitives that determines how it is accessed. Private means class only. No specific means package only. Public means any class can access it. There are also some other modifiers(ex: protected) but those are rarely used.
 
So ` static long scaleFactor = 1f;` is ok? All my fields like for example ` static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];` are static
Lunar, maybe we could talk on chat here someday instead of writing it all here? There is some way to create a private chat for specific conversation, right?
 
7:19 PM
Static is in general a bad idea to use. If you have zoomed, and then return, the zoom factor will not change. Static = doesn't change to a default value after being initialized when the app is started.
 
Oh..that is interesting :)
all thred is here now
Ok..let's start.
but first of all I'd like to thank you a lot!!!!
so what do you suggest ?
 
Don't use static firstly, use private if you don't use it outside the class. Use none if you use it in the package, and use public if you use it outside the package. This is up to you based on your needs and your code, but I would take a guess and say "private" is what you need. If you use it somewhere and it has the wrong modifier, Android Studio will show an error telling you that it needs a different access modifier
 
public class MainActivity extends AppCompatActivity {

private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
this is the beginning of java file
 
answered checked and isAnswered can probably be non-static. NUMBER_OF_QUESTIONS can be static final
 
public class MainActivity extends AppCompatActivity {

private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
private scaleFactor = 1f;
 
7:23 PM
there should be a "fixed font" button when you paste code. Press it to make sure you can fit the code there
 
where can i find the fixed font?
 
It is a chat-feature. If you paste code you should see a button near "send" and "upload" that says "fixed font"
 
public class MainActivity extends AppCompatActivity {

    private static int NUMBER_OF_QUESTIONS = 3;
    static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
    static boolean[] checked = new boolean[NUMBER_OF_QUESTIONS];
    static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
    private scaleFactor = 1f;
ok got it.
 
Anyways, the code looks good. Did you manage to implement zoom?
 
To be honest with you this is just the first line I've implemented to this MainActivity:( I read a bit of extra info regarding to what you posted and acutally now I'm starting..
Imagine me being in a middle of a maze where you don't know which way to go:)
that is how I feel right now
I'm so eager to learn but this zoom...is wow! If i just copy your code there will be no use for me at all. I just have to go line by line to fully understand what I'm doing.
so ScaleGestureListener goes to onCreate?
 
7:50 PM
or maybe inside the viewPager since it is for handling Fragments
 
ScaleListener and Scaler(the two classes) will be pasted into the class as nested classes. They do not go in a specific method. The initializers though, do
 
(Your knowledge shocks me in a positive way)..so nested means inside of any class?
 
It means you create a new class inside an existing class, without creating a new file.
So basically copy-paste the code outside methods. Or write [ANY_ACCESS_MODIFIER] classalong with the class name and brackets you can create nested classes.
 
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            class Scaler extends ScaleGestureDetector {
                public Scaler(Context context, OnScaleGestureListener listener) {
                    super(context, listener);
                }

                @Override
                public float getScaleFactor() {//Leave this method empty.
                    return super.getScaleFactor();
                }
            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
like so?
 
No, the onPageChangeListener is something else.
Here is an example, but not with the appropriate code, but yo uget the idea:
public class YourActivity extends Activity{
    public void onCreate(Bundle sis){
        super.onCreate(sis);
        ...
    }
    class Scaler{

    }

    class ScaleListener{

    }
}
 
8:05 PM
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonCheckAnswer = (Button) findViewById(R.id.button_check_answers);


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            class Scaler extends ScaleGestureDetector {
                public Scaler(Context context, OnScaleGestureListener listener) {
                    super(context, listener);
                }
 
Not exactly. Find the final bracket of onCreate. There you paste the scaler class
It doesn't go into a specific listener
 
Seems to be ok. Just Scaler never used but it will be fixed
why we have to put this method at the end of onCreate?
 
It doesn't have to be after onCreate. It is just a relative measure so you understand where it goes. It doesn't go in a method, but inside the class.
 
so now we have this
class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener{

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scaleFactor *= detector.getScaleFactor();

        if(scaleFactor > 2) scaleFactor = 2;//Limit to your liking
        else if(scaleFactor < 0.3f) scaleFactor = 0.3f;//Limit to your liking
        scaleFactor = (scaleFactor * 100) / 100;//jitter-protection
        //scaleMatrix.setScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());//This is for usage with a Matrix: Good for canvas and other areas where this is 
 
8:20 PM
I will be leaving now, so I will leave you with the next steps. Set an onTouchListener on the view you want to zoom. Initialize scaler and listener(see the answer), and copy the scaling into the onTouchListener. It is critical it is an onTouchListener. onClickListener will not do the same job.
I have explained in the answer where the specific pieces go after that, so you should be fine. I will come back in about 18 hours. The current code looks good. Replace tv with the view you want to scale.
 
Ok Lunar. Thank you so much. Have a good sleep. See you soon.
 

  last day (16 days later) »