IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Encore un problème avec AnalogClock


Sujet :

Android

  1. #1
    Candidat au Club
    Femme Profil pro
    Inscrit en
    Juin 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juin 2011
    Messages : 5
    Points : 4
    Points
    4
    Par défaut Encore un problème avec AnalogClock
    Bonsoir,

    Ce post fait suite à celui-ci : http://www.developpez.net/forums/d10...-analog-clock/

    La méthode de Viish marche bien dans le cadre d'une vue en portrait et pour une table de 3x2, mais dès que je passe en landscape ou que je passe en 3x3 (et en changeant les ratios) plus rien ne va.

    Voici ma classe :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    public class AnalogClock extends View 
    {
        private Time mCalendar;
        private Context mContext;
     
        private Drawable mHourHand;
        private Drawable mMinuteHand;
        private Drawable mDial;
     
        private int mDialWidth, mDialHeight;
        private int mScreenWidth = 0, mScreenHeight = 0;
     
        private boolean mAttached;
     
        private final Handler mHandler = new Handler();
        private float mMinutes;
        private float mHour;
        private boolean mChanged;
     
        public AnalogClock(Context context) 
        {
            this(context, null);
        }
     
        public AnalogClock(Context context, AttributeSet attrs) 
        {
            this(context, attrs, 0);
        }
     
        public AnalogClock(Context context, AttributeSet attrs, int defStyle) 
        {
            super(context, attrs, defStyle);
            mContext = context;
            Resources r = context.getResources();
     
            mDial = r.getDrawable(R.drawable.clock_dial);
            mHourHand = r.getDrawable(R.drawable.clock_hour);
            mMinuteHand = r.getDrawable(R.drawable.clock_minute);
     
            mCalendar = new Time();
            mDialWidth = mDial.getIntrinsicWidth();
            mDialHeight = mDial.getIntrinsicHeight();
        }
     
        public void setCalendar(Time calendar)
        {
        	mCalendar = calendar;
        	onTimeChanged();
            invalidate();
        }
     
        public void setScreenDimensions(int w, int h)
        {
        	mScreenWidth = w;
        	mScreenHeight = h;
            invalidate();
        }
     
        protected void onAttachedToWindow() 
        {
            super.onAttachedToWindow();
     
            if (!mAttached) 
            {
                mAttached = true;
                IntentFilter filter = new IntentFilter();
     
                filter.addAction(Intent.ACTION_TIME_TICK);
                filter.addAction(Intent.ACTION_TIME_CHANGED);
                filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
     
                getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
            }
     
            onTimeChanged();
        }
     
        protected void onDetachedFromWindow() 
        {
            super.onDetachedFromWindow();
            if (mAttached) 
            {
                getContext().unregisterReceiver(mIntentReceiver);
                mAttached = false;
            }
        }
     
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
        {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSize = /*MeasureSpec.getSize(widthMeasureSpec);*/ mScreenWidth / Clock.COLS;
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = /*MeasureSpec.getSize(heightMeasureSpec);*/ mScreenHeight / Clock.ROWS;
     
            float hScale = 1.0f;
            float vScale = 1.0f;
     
            if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) 
                hScale = (float) widthSize / (float) mDialWidth;
     
            if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) 
            	vScale = (float) heightSize / (float) mDialHeight;
     
            float scale = Math.min(hScale, vScale);
     
            setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec), resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
        }
     
        protected void onSizeChanged(int w, int h, int oldw, int oldh) 
        {    	
        	mScreenWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();
        	mScreenHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
     
            super.onSizeChanged(w, h, oldw, oldh);
            mChanged = true;
        }
     
        protected void onDraw(Canvas canvas) 
        {
            super.onDraw(canvas);
     
            boolean changed = mChanged;
            if (changed)
                mChanged = false;
     
            int availableWidth = mScreenWidth / Clock.COLS;
            int availableHeight = mScreenHeight / Clock.ROWS;
     
            int x = availableWidth / 2;
            int y = availableHeight / 2;
     
            final Drawable dial = mDial;
            int w = dial.getIntrinsicWidth();
            int h = dial.getIntrinsicHeight();
     
            boolean scaled = false;
     
            if (availableWidth < w || availableHeight < h) 
            {
                scaled = true;
                float scale = Math.min((float) availableWidth / (float) w, (float) availableHeight / (float) h);
                canvas.save();
                canvas.scale(scale, scale, x, y);
            }
     
            if (changed) 
                dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
     
            dial.draw(canvas);
     
            canvas.save();
            canvas.rotate(mHour / 12.0f * 360.0f, x, y);
            final Drawable hourHand = mHourHand;
            if (changed) 
            {
                w = hourHand.getIntrinsicWidth();
                h = hourHand.getIntrinsicHeight();
                hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
            }
            hourHand.draw(canvas);
            canvas.restore();
     
            canvas.save();
            canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);
     
            final Drawable minuteHand = mMinuteHand;
            if (changed) 
            {
                w = minuteHand.getIntrinsicWidth();
                h = minuteHand.getIntrinsicHeight();
                minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
            }
            minuteHand.draw(canvas);
            canvas.restore();
     
            if (scaled) 
                canvas.restore();
        }
     
        private void onTimeChanged() 
        {
            mCalendar.setToNow();
     
            int hour = mCalendar.hour;
            int minute = mCalendar.minute;
            int second = mCalendar.second;
     
            mMinutes = minute + second / 60.0f;
            mHour = hour + mMinutes / 60.0f;
            mChanged = true;
        }
     
        private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() 
        {
            public void onReceive(Context context, Intent intent) 
            {
    //            if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) 
    //            {
    //                String tz = intent.getStringExtra("time-zone");
    //                mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
    //            }
     
                onTimeChanged();
                invalidate();
            }
        };
    }
    Ci joint quelques screens illustrant mon problème.

    Alors que ma vue est alignée en CENTER, j'ai l'impression que lors du onDraw de l'horloge elle ne se dessine pas au milieu de la zone réservée (TableLayout puis LinearLayout par couple TextView + Horloge).

    Merci d'avance pour votre aide.
    Images attachées Images attachées   

  2. #2
    Candidat au Club
    Femme Profil pro
    Inscrit en
    Juin 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juin 2011
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Personne ne sait comment rendre ma vue personnalisée centrée dans la zone impartie par mon table layout svp ?

  3. #3
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Points : 4 240
    Points
    4 240
    Par défaut
    Tes variables Clock.COLS et Clock.ROWS sont définies et initialisées comment ?
    Si vous jugez mon post utile dans la résolution de votre problème, n'hésitez pas à utiliser le système de vote afin d'améliorer la qualité du forum

  4. #4
    Candidat au Club
    Femme Profil pro
    Inscrit en
    Juin 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juin 2011
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Bonjour,

    Voici mon code initialisant mes variables en fonction de l'orientation du téléphone :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public static int ROWS = 3;
    public static int COLS = 2;
    ...
    if (portrait)
    {
        	ROWS = 3; COLS = 2;
    }
    else
    {
        	ROWS = 2; COLS = 3;
    }
    Merci pour ton aide !

  5. #5
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Points : 4 240
    Points
    4 240
    Par défaut
    Et tu es sûr que ces variables sont bien initialisés comme il faut au bon moment ?
    Si vous jugez mon post utile dans la résolution de votre problème, n'hésitez pas à utiliser le système de vote afin d'améliorer la qualité du forum

Discussions similaires

  1. Encore des problèmes avec le BDE
    Par Flint dans le forum C++Builder
    Réponses: 19
    Dernier message: 31/12/2007, 23h26
  2. Urgent : Encore un problème avec un DbDataReader
    Par Nadiya dans le forum Windows Forms
    Réponses: 6
    Dernier message: 11/06/2007, 09h03
  3. Encore un problème avec Scyte et gets
    Par alixbasix dans le forum Ruby
    Réponses: 10
    Dernier message: 02/03/2007, 15h25
  4. Réponses: 6
    Dernier message: 22/01/2007, 11h43
  5. Encore un problème avec netscape !!
    Par Death83 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 06/02/2006, 21h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo